我试图找到但找不到,所以我来了。我想了解是否有任何方法可以检测路由更改和 url 条目之间的差异。我有一些场景需要对路由更改和网址条目做出不同的响应 - 例如点击书签中的链接。
答案 0 :(得分:1)
我在 app.component
中写了这样的东西,它按预期工作:
changeCount = 0;
constructor(private router: Router) {
this.subs.add(this.router.events.subscribe((event: Event) => {
if (event instanceof NavigationEnd) {
this.changeCount++;
if (this.changeCount == 1) {
// do some works
} else {
// do some other works
}
}
if (event instanceof NavigationError) {
this.router.navigateByUrl("/");
}
}));
}
如果changeCount
等于1,则表示这是第一个进入网站的条目,如果大于1,则表示这是路线变更。