Angular UI路由器$ transitions.onBefore [英] Angular UI router $transitions.onBefore
本文介绍了Angular UI路由器$ transitions.onBefore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是角度ui路由器版本1.0.0-alpha.4,并且由于默认情况下根作用域事件是不活动的,因此我想到了使用新的实现身份验证机制的方法,如下所示.
I am using angular ui router version 1.0.0-alpha.4 and since by default root scope events are inactive I thought of using new way of implementing auth mechanism as follows.
.run(['$transitions', '$timeout', '$q', '$state', 'AuthService', ($transitions, $timeout, $q, $state, AuthService) => {
$transitions.onBefore({ to: 'app.*', from: '*' }, () => {
const deferred = $q.defer();
AuthService.isLoggedIn().then(() => {
deferred.resolve()
}, () => {
// redirect to error page
// how can i redirect user to error page. $state.go('error') not working.
deferred.reject()
});
return deferred.promise;
}, {priority: 10});
}])
但是,问题是我不确定如何将用户重定向到错误页面.我尝试使用$ state.go('error'),似乎不起作用.有什么建议?
However issue is that I am not sure how to redirect user to error page. I try to use $state.go('error') and seems does not working. Any suggestions?
推荐答案
很酷,我找到了解决方案.将其发布给可能提出相同问题的任何人.您所需要做的就是如下解决诺言.
Cool I found the solution. Posting this to anyone who may come up with the same issue. All you need is resolve the promise as follows.
deferred.resolve($state.target('error', undefined, { location: true }))
这篇关于Angular UI路由器$ transitions.onBefore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文