Issue
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);
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...
Solution
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: ', this.router.url.toString());
});
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.
Answered By - user6749601
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.