当前位置:   article > 正文

Angular4 监听路由URL的变化_angular4 监听路由变化

angular4 监听路由变化

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

支持的事件类型:

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

在判断事件类型需要导入对应的事件类型,如:

import { Router, NavigationStart } from '@angular/router';

监听单一事件

  1. this.router.events
  2.   .filter((event) => event instanceof NavigationEnd)
  3.   .subscribe((event:NavigationEnd) => {
  4.     //do something
  5. });

监听多个事件

  1. constructor(router:Router) {
  2.   router.events.subscribe(event:Event => {
  3.     if(event instanceof NavigationStart) {
  4.       //
  5.     } else if(event instanceof NavigationEnd) {
  6.       //
  7.     } else if(event instanceof NavigationCancel) {
  8.       //
  9.     } else if(event instanceof NavigationError) {
  10.       //
  11.     } else if(event instanceof RoutesRecognized) {
  12.       //
  13.     }
  14.   });
  15. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/222857
推荐阅读
相关标签
  

闽ICP备14008679号