What all could be the reasons for a NavigationCancel event to get triggered?

半城伤御伤魂 提交于 2021-02-07 06:42:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!