【问题标题】:Ui-router: How to get next state params in transitionUi-router:如何在转换中获取下一个状态参数
【发布时间】:2016-10-25 14:33:20
【问题描述】:

我正在使用 ui-router 1.0.0.beta.3。如何在转换期间获取下一个状态的路由参数?

index.run.js

$transitions.onStart({ to: '**' }, verifyAuth);

function verifyAuth(trans) {
  let nextState = trans.$to();
  if (Auth.verify(nextState.authGroup) === -1) {      
    return $state.go('login', { nextState: nextState.name, nextParams: nextState.params}); // this doesn't work
  }
}

我想存储它们,以便在身份验证成功后将用户重定向到他尝试访问的状态 + 状态参数。

login.component.js

let nextState = this.$stateParams.nextState;
let nextParams = this.$stateParams.nextParams;
$state.go(nextState, nextParams);

【问题讨论】:

    标签: javascript angularjs angular-ui-router


    【解决方案1】:

    您的问题的答案是Transition.params()

    function verifyAuth(trans) {
      let nextState = trans.to();
      let nextParams = trans.params();
      if (Auth.verify(nextState.authGroup) === -1) {      
        return $state.go('login', { nextState: nextState.name, nextParams: nextParams});
      }
    }
    

    但是,我建议研究ui-router 1.0 sample app 如何执行身份验证检查和登录重定向:

    这个钩子通过返回一个$state.target('login')将未验证的转换重定向到登录状态

    https://github.com/ui-router/sample-app-ng1/blob/master/app/global/requiresAuth.hook.js#L15-L24

    在登录状态下,确定“登录后返回的状态”。它使用Transition.redirectedFrom() 检查原始转换的目的地。

    https://github.com/ui-router/sample-app-ng1/blob/master/app/main/app.states.js#L44-L83

    【讨论】:

    • 很好的答案,但是,作为附录,如果您遇到无法一次性处理那么多重构的遗留项目,我发现在 1.0.5 上我可以使用 trans.$to()trans.$from() 作为替代品。希望我可以在某个时候再次对其进行重构,以配合示例应用的策略。
    • 我正在拼命寻找这个。对没有正确使用谷歌搜索并跳转到他们的库的源代码感到愤怒。他们有不必要的复杂文档和库设计。
    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2014-08-21
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多