Angular 4检测路由变化,可使用router.events来监听:html
支持的事件类型:this
在判断事件类型须要导入对应的事件类型,如:code
import { Router, NavigationStart } from '@angular/router';
监听单一事件router
this.router.events .filter((event) => event instanceof NavigationEnd) .subscribe((event:NavigationEnd) => { //do something });
监听多个事件htm
constructor(router:Router) { router.events.subscribe(event:Event => { if(event instanceof NavigationStart) { // } else if(event instanceof NavigationEnd) { // } else if(event instanceof NavigationCancel) { // } else if(event instanceof NavigationError) { // } else if(event instanceof RoutesRecognized) { // } }); }
运用实例参考:http://www.javashuo.com/article/p-ytyoqxcu-dt.html