UI-Router:创建规则以防止访问状态 [英] UI-Router: create rules to prevent access to a state

查看:25
本文介绍了UI-Router:创建规则以防止访问状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UI 路由器常见问题解答,他们提出了一些基于 data.rule 状态方法来阻止访问某些状态的方法.假设当前用户由某个 $currentUser 服务确定.

In UI-router FAQ, they suggested some way for preventing access to some states based on data.rule method of states. Assuming current user is determined by some $currentUser service.

然而,这个假设在现实世界的用例中似乎并不成立.因为通常这个 $currentUser 服务的实现方式是从服务器查询当前用户,它可能返回的最好的方式是 promise当前用户的.因此,在触发$stateChangeStart事件的那一刻,这个promise很有可能还没有被解决.

However this assumption doesn't seem to be true in real world use cases. Because normally this $currentUser service is implemented in a way that it queries current user from the server and probably the best it can return is a promise of the current user. Therefore, at the moment that $stateChangeStart event is triggered, it's quite possible for this promise to not be resolved yet.

考虑到上述讨论,最好的做法是什么?

So what's the best thing to do, considering the above discussion?

一种解决方法可能是在每个安全状态中包含一个 resolve 属性,并带有一个返回当前用户承诺的函数.但我真的不喜欢它,因为它有点打破了我心中的封装.

One workaround might be to include a resolve property in every secured state, with a function that returns the promise of the current user. But I really don't like it because it kinda breaks encapsulation in my mind.

推荐答案

这里的解决方案是推迟 UI-Router 执行 - 直到 $currentUser 被解决.该技术可以从内置功能中受益:deferIntercept(defer)

Solution here would be to postpone UI-Router execution - until $currentUser is resolved. The technique could profit from built in feature: deferIntercept(defer)

deferIntercept(defer)

禁用(或启用)延迟位置更改拦截.

Disables (or enables) deferring location change interception.

如果您希望自定义同步 URL 的行为(例如,如果您希望延迟转换但保留当前 URL),请在配置时调用此方法.然后,在运行时,在您配置自己的 $locationChangeSuccess 事件处理程序后调用 $urlRouter.listen().

If you wish to customize the behavior of syncing the URL (for example, if you wish to defer a transition but maintain the current URL), call this method at configuration time. Then, at run time, call $urlRouter.listen() after you have configured your own $locationChangeSuccess event handler.

所有细节都可以在这里找到和观察

All the details could be found and observed here

这篇关于UI-Router:创建规则以防止访问状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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