最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get navigation history? - Stack Overflow

programmeradmin5浏览0评论

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?

Share Improve this question edited Mar 5, 2019 at 9:44 Makyen 33.4k12 gold badges92 silver badges125 bronze badges asked Mar 5, 2019 at 9:06 statlerstatler 1,3812 gold badges16 silver badges27 bronze badges 3
  • 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
Add a ment  | 

1 Answer 1

Reset to default 3

I 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.

发布评论

评论列表(0)

  1. 暂无评论