角路由器事件:NavigationEnd-如何仅过滤最后一个事件 [英] Angular Router Events: NavigationEnd -- How to filter only the last event
问题描述
我只想从router.events中提取 NavigationEnd类型的最后一个事件条目.但是不知何故我找不到合适的方法来做到这一点.无论我如何尝试,以下代码片段都是访问这些感兴趣的对象的唯一方法.
I want to extract only the last event entry of type NavigationEnd from router.events. But somehow I can't find a proper way to do this. Whatever I try the following code snipped is the only way to get access to these objects of interest.
let ne: any;
router.events.filter(e => e instanceof NavigationEnd)
.forEach(e => {
ne = e as NavigationEnd;
});
console.log('target page: ', ne.urlAfterRedirects);
但是我真的必须使用.forEach()让它在所有条目中运行,然后使用最后一个条目吗?没有更好的方法将事件作为一种数组来处理,然后说
But do I really have to use .forEach() letting it run though all entries and using then the last entry? Isn't there a better way to handle the events as a kind of array and then say
ne = arrayOfEvents[arrayOfEvents.length-1]
?
我对此感到疯狂,但似乎看不到树木的木头...
I'm getting crazy about it but it seems that I can't see the wood for the trees...
推荐答案
好吧,在阅读完上面发布的文章并重新思考我真正想要实现的目标后,我发现我的方法肯定太复杂了.因为实际上我只需要访问当前调用的路由.因此,我从头开始,遇到了这个小而有效的解决方案:
Okay, after reading through the posted articles above and rethinking what I really want to achieve I found out that my approach was definitely too complicated. Because actually I only need access to the currently called route. And so I started from scratch and came across this small but very effective solution:
this.router.events.subscribe(value => {
console.log('current route: ', router.url.toString());
});
感谢所有在oder中发表评论的人支持我!因为我能够绑住松散的末端,所以它很有帮助.
Thanks to all who shared a comment in oder to support me! It helped al lot as I was able to tie up loose ends.
这篇关于角路由器事件:NavigationEnd-如何仅过滤最后一个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!