如何在 navigationStart 路由器事件中暂停路线更改 [英] How to pause the route change in navigationStart router event

查看:44
本文介绍了如何在 navigationStart 路由器事件中暂停路线更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有从包(节点模块)中公开的路由.所以我不能对节点模块内定义的路由使用 canActivate 或 canDeactivate.

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.

由于条件有 API 调用,路由更改不会在 navigationStart 中暂停,而是完成到其他页面的路由,一旦 API 调用成功,重定向就会发生在其他路由上.

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.

我该如何处理?无论如何要暂停 navigationStart 中的路由更改并在 API 调用后启用路由,或者是否有任何其他方法可以为所有路由(包括来自节点模块的路由)定义 canActivate.

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.

推荐答案

是的,通过使用解析器,您可以实现这一点,

Yes, by using resolver you can achieve this,

  1. 您需要先创建解析器服务,该服务将实现解析"接口.

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

在解析函数中,进行 API 调用.

Inside the resolve function, make the API call.

并像这样在路由声明中添加服务=>解析:{数据:解析服务}

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

所以,在调用 API 并且没有返回数据之前,它不会启动 navigationStart请看看这篇文章https://codeburst.io/understanding-resolvers-in-angular-736e9db71267

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

这篇关于如何在 navigationStart 路由器事件中暂停路线更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆