何时在Angular 2中启动NavigationStart? [英] When is NavigationStart fired in Angular 2?

查看:356
本文介绍了何时在Angular 2中启动NavigationStart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从外部站点重定向到此站点后,是否会在页面加载时触发NavigationStart事件?

Is NavigationStart event fired on page load, after redirection has occurred from an external site to this site?

从外部站点重定向到SomeComponent时不会触发的示例代码.

Sample code which is not fired on redirection from external site to SomeComponent.

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

export class SomeComponent { 
   constructor(public _router: Router, private _activeRoute: ActivatedRoute,    private _location: Location) {
     this.router = _router; 
     this.router.events
         .filter(e => e instanceof   NavigationStart)     
         .pairwise()
         .subscribe((e) => { alert(e); }); 
   }
}

推荐答案

在导航过程中创建和添加的组件内,您不会收到有关NavigationStart的通知. 而是将代码添加到AppComponent

You can't get notified about NavigationStart inside the component that is created and added during navigation. Instead add the code to AppComponent

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

export class AppComponent { 
   constructor(public _router: Router, private _activeRoute: ActivatedRoute,    private _location: Location) {
     this.router = _router; 
     this.router.events
         .filter(e => e instanceof   NavigationStart)     
         .pairwise()
         .subscribe((e) => { alert(e); }); 
   }
}

这篇关于何时在Angular 2中启动NavigationStart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆