angular路由事件

Angular 4检测路由变化,可使用router.events来监听:html

支持的事件类型:this

  • NavigationStart:导航开始
  • NavigationEnd:导航结束
  • NavigationCancel:取消导航
  • NavigationError:导航出错
  • RoutesRecoginzed:路由已认证

在判断事件类型须要导入对应的事件类型,如: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
相关文章
相关标签/搜索