码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular] Subscribing to router events

时间:2017-04-25 10:09:43      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:scribe   div   logs   port   ini   outer   enable   bin   angular   

In our root component, one thing we can do is subscribe to Router events, and do something related to router events. So how can we get router event?

 

export class AppComponent implements OnInit {
  constructor(private router: Router) {}
  ngOnInit() {
    this.router.events
      .subscribe(events => console.log(events))
  }
}

To events we log out from ‘this.router.events‘ is actually the same as we enable router tracing.

 

If you only interested in one event ‘NavigationEnd‘, we can use RxJS filter:

import rxjs/add/opreator/fitler;

export class AppComponent implements OnInit {
  constructor(private router: Router) {}
  ngOnInit() {
    this.router.events
      .filter(event => event instanceof NavigationEnd)
      .subscribe(events => console.log(events))
  }
}

 

Here we use ‘instanceof‘, this is because which event is a class. So we can use instanceof to filter according to the class.

 

[Angular] Subscribing to router events

标签:scribe   div   logs   port   ini   outer   enable   bin   angular   

原文地址:http://www.cnblogs.com/Answer1215/p/6760017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!