问题描述
我需要你的帮助来获取之前的路线.我正在使用下面的链接来获取它,但它不会在第一次触发...
I need your assistance to get the previous route path. I am using the below link to get that but it doesn't trigger at first time...
constructor(router: Router) {
router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe((event: NavigationEnd) => {
console.log('prev:', event.previousUrl);
this.previousUrl = event.url;
});
}
我同时使用了 RoutesRecognized 和 NavigationEnd,但没有使用..
I used both RoutesRecognized and NavigationEnd but didn't get that..
我在构造函数和 ngOnInit 中调用了这个方法,但是每次第一次都没有触发......
I called this method in constructor and ngOnInit but every time it doesn't trigger at first time....
有什么办法吗,我能搞定...
is there any way, I can get that...
谢谢...
推荐答案
Router
类公开了一个 getCurrentNavigation()
方法:
The Router
class exposes a getCurrentNavigation()
method:
getCurrentNavigation(): Navigation|null {
return this.currentNavigation;
}
Router.currentNavigation
属于 Navigation
类型:
private currentNavigation: Navigation|null = null;
正如我们从Navigation
的定义中看到的,您可以使用previousNavigation
属性:
As we can see from Navigation
's definition, you can access the previous navigation with the previousNavigation
property:
previousNavigation: Navigation | null;
因此,您可以使用此属性获取先前的路径.
So, you could use this property to get the previous path.
这篇关于Angular 9 如何获取之前的路由路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!