How to pause the route change in navigationStart router event

霸气de小男生 提交于 2019-12-24 10:59:57

问题


In my application I have routes exposed from packages (node modules). So i couldn't use canActivate or canDeactivate for the routes defined inside node modules.

So I started subscribing to all route change in my app component and based on condition i am redirecting the user to different routes.

Since the condition has API call, the route change is not paused in the navigationStart instead it completes the routing to other page and once API call is successful the redirect happens to the other route.

How can i handle this? Is there anyway to pause the route change in navigationStart and enable the route after the API call or is there any other way to define canActivate for all routes including the routes from node modules.


回答1:


Yes, by using resolver you can achieve this,

  1. You need to create resolver service first which will implement "Resolve" interface.

  2. Inside the resolve function, make the API call.

  3. And add the service inside the route declarations like this => resolve: {data: ResolveService}

  4. So, until the API call is made and it doesn't return the data, it won't start the navigationStart Please have a look at this article https://codeburst.io/understanding-resolvers-in-angular-736e9db71267



来源:https://stackoverflow.com/questions/52010806/how-to-pause-the-route-change-in-navigationstart-router-event

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