问题
I have a subscription for router events in angular2, When I console the events, I find a NavigationCancel event with reason: "". Curious to know what all could be the reasons for a NavigationCancel to get triggered. Not only with an empty reason but in general.
回答1:
NavigationCancel will be triggered when you are trying to naviggate to a route and navigated route children can not be loaded (CanLoad guard) or route itself cannot be activated (CanActivate guard)
you may use {enableTracing : true} while configuring RouterModule to see all events, and analyze further.
Hope this helps!!
回答2:
This is late, but i hope it might help someone else, as I had struggled with this also.
So, another reason this could be happening is that the navigation event is triggered twice.
This happened to me by mistake, because i had a component with a routerLink directive that had an input binding with the same name:
my-component Class:
@Input() routerLink: string[];
my-component Template:
...
<a [routerLink]="routerLink">
...
The parent component template was like this :
...
<my-component
[routerLink]="somePath"
...
>
Which was actually creating another routerLink directive binding.
As a result the navigation event was firing twice, almost simultaneously, which produced the NavigationCancel event.
来源:https://stackoverflow.com/questions/44371818/what-all-could-be-the-reasons-for-a-navigationcancel-event-to-get-triggered