Ui-router:如何在转换中获得下一个状态参数

Dan*_*e A 10 javascript angularjs angular-ui-router

我正在使用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
  }
}
Run Code Online (Sandbox Code Playgroud)

我想存储它们,以便在认证成功后,我可以将用户重定向到他试图访问的状态+状态参数.

login.component.js

let nextState = this.$stateParams.nextState;
let nextParams = this.$stateParams.nextParams;
$state.go(nextState, nextParams);
Run Code Online (Sandbox Code Playgroud)

Chr*_*s T 15

你的问题的答案是 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});
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,我建议研究ui-router 1.0示例应用程序如何执行身份验证检查和登录重定向:

此挂钩通过返回a 将未经身份验证的转换重定向到登录状态$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