Marc Rasmussen
Marc Rasmussen

Reputation: 20555

Angular / ionic Check for same page navigation event

I have a bottom menu that contains a few buttons that each link to a page using navigate:

this.navigate(TimelinePath, 'back');

Then I have an overlay that checks for the path to test if it should close:

this.routerSubscription = this.router.events.subscribe((val) => {
    if (val instanceof NavigationEnd) {
        this.closeModal();
    }
});

This works, however there is an issue with it: If I am already standing on the page I try to redirect to, this event is not fired.

So my question is: How can I check whether the page that should be redirected to is already opened?

Upvotes: 1

Views: 1414

Answers (1)

Guerric P
Guerric P

Reputation: 31825

The key is the onSameUrlNavigation option that exists since Angular 5, in the RouterModule import.

RouterModule.forRoot(routes, {
    onSameUrlNavigation: 'reload'
})

This will trigger the NavigationEnd and close your modal even if navigating to the same URL. Check this StackBlitz to see it in action.

Upvotes: 3

Related Questions