加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何重定向在ui路由器解析?

发布时间:2020-12-17 08:35:09 所属栏目:安全 来源:网络整理
导读:我试图重定向内部的ui路由器解决方案,想知道是否有一种方法在路由器解析器重新路由。目前这不工作,因为一个人会想。 resolver(auth,$state){ if(!auth.isLoggedIn()){ $state.go('noLoggedInPath'); }} 如何在解析器中正确重定向? 我的temp hack是这个,
我试图重定向内部的ui路由器解决方案,想知道是否有一种方法在路由器解析器重新路由。目前这不工作,因为一个人会想。
resolver(auth,$state){
   if(!auth.isLoggedIn()){
       $state.go('noLoggedInPath');
   }
}

如何在解析器中正确重定向?

我的temp hack是这个,但我不舒服。

resolver(auth,$state,$timeout){
   if(!auth.isLoggedIn()){
        $timeout(function () {

             $state.go('noLoggedInPath');
        },0);
   }
}
您可以从解析器函数返回一个promise,它将指示是否继续导航到状态。如果你决定在别的地方导航 – 拒绝承诺并指定适当的状态:
resolver($q,$timeout,auth) {
    var deferred = $q.defer();

    // $timeout is an example; it also can be an xhr request or any other async function
    $timeout(function() {
      if (!auth.isLoggedIn()) {
        // user is not logged,do not proceed
        // instead,go to a different page
        $state.go('noLoggedInPath');
        deferred.reject();
      } else {
        // everything is fine,proceed
        deferred.resolve();
      }
    });

    return deferred.promise;
}

Plunkr here。

UPD:更新代码和添加plunkr。看起来只有当你把它放在一个超时函数。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读