[英]Ui-router: How to get next state params in transition
我正在使用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);
你的问题的答案是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示例应用程序如何执行身份验证检查和登录重定向:
此挂钩通过返回$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.