admin管理员组文章数量:1399747
I am trying to implement a menu including the most recently visited and most monly visited pages within the Angular app. How can I get the navigation stack? Or do I need to hook the navigationStart
event and pile it as it is created?
I am trying to implement a menu including the most recently visited and most monly visited pages within the Angular app. How can I get the navigation stack? Or do I need to hook the navigationStart
event and pile it as it is created?
- What is the scope? Do you want to track most popular pages for the current user? all users? all tabs/windows? – umutesen Commented Mar 5, 2019 at 9:28
- At the moment Angular doesn't have built-in possibility to get all navigation history. You need to use navigation events to store navigation history for further needs. – taras-d Commented Mar 5, 2019 at 10:00
- you can store all visited route. your question answers is this link – mosi98 Commented Mar 5, 2019 at 10:22
1 Answer
Reset to default 3I don't think it is possible to get the full history with a simple method call, but you can track this easily yourself by subscribing to the NavigationEnd.
previousUrl: string;
constructor(router: Router) {
router.events
.filter(event => event instanceof NavigationEnd)
.subscribe(e => {
console.log('prev:', this.previousUrl);
this.previousUrl = e.url;
});
}
In this sample it saves only the previous route, but you could store in an array to save all the previously visited routes.
See also: How to determine previous page URL in Angular?
Update:
You can use the above code in bination with the code below to subscribe to the NavigationEnd in your service from the start of your application.
export class AppModule {
constructor(navigationEndService: NavigationEndService) {
navigationEndService.init();
}
To get hold of the list in other places you could create a getter in the service.
本文标签: javascriptHow to get navigation historyStack Overflow
版权声明:本文标题:javascript - How to get navigation history? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744198576a2594856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论