2016-10-25 33 views
6

我正在使用ui-router 1.0.0.beta.3。如何在轉換期間路由下一個狀態的參數?Ui路由器:如何獲得轉換中的下一個狀態參數

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 
    } 
} 

我要存儲這些,所以我可以將用戶重定向到狀態+狀態PARAMS他試圖接入,認證成功後。

login.component.js

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

回答

9

的回答你的問題是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

+0

很好的答案,但,作爲附錄,如果你堅持的傳統項目,無法處理這麼多的重構一氣呵成,在1.0.5我發現我可以用'反式。$將()'和'trans。$ from()'作爲替換放置。希望我可以再次重構它,以便隨着示例應用程序的策略。 – toxaq

相關問題