簡體   English   中英

ui-router:onEnter和onStart之間的區別?

[英]ui-router: Difference between onEnter and onStart?

我正在切換到新版本的ui-router (1.0.0-alpha.5),並試圖弄清楚在哪里使用onEnter鈎子和onStart

$transitions.onStart()

$transitions.onEnter()

之前只是事件$ stateChangeStart

以下是doc如何描述轉換生命周期掛鈎執行順序:

onBefore
onStart/onInvalid
onEnter (for individual states)
onSuccess
onError

......但它似乎有點過時(我稍后再回過頭來看)。 盡管如此,它清楚地表明onEnter掛鈎是關於進入一個狀態onStart掛鈎是關於開始狀態之間的轉換

實際上, 鈎子文檔頁面中描述了這個關鍵差異:

當Transition開始運行時,按優先級順序異步調用onStart掛鈎。 此時,過渡尚未退出或進入任何州。

當Transition進入狀態時,按優先級順序異步調用onEnter掛鈎。 onRetain鈎子之后輸入onRetain

onStart hook似乎是驗證轉換的好地方 - 例如,檢查用戶是否經過身份驗證。 這是該doc中給出的代碼示例:

$transitions.onStart( { to: 'auth.*' }, function(MyAuthService, $state) {
  // If the user is not authenticated
  if (!MyAuthService.isAuthenticated()) {

    // Then return a promise for a successful login.
    // The transition will wait for this promise to settle
    return MyAuthService.authenticate().catch(function() {

      // Redirect to a state that we know doesn't require auth.
      return $state.target("guest");
    });
  }
});

從最后一頁來看,訂單實際上是:

onBefore - Transition is about to start; one can register other hooks here "on-the-fly"
onStart  - Transition starts running
onExit   - Transition is exiting a state
onRetain - Transition retains a state
onEnter  - Transition is entering a state 
onFinish - Transition is about to be completed, all states are entered and exited

onSuccess/onError - Transition is completed (either successfully or not)

注意:除了最后兩個鈎子之外的所有鈎子都可以修改轉換 - 更改目標狀態等onSuccessonError在事后發生:轉換結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM