gpt4 book ai didi

javascript - 用户界面路由器 : Difference between onEnter and onStart?

转载 作者:搜寻专家 更新时间:2023-11-01 05:21:17 26 4
gpt4 key购买 nike

我正在切换到新版本的 ui-router (1.0.0-alpha.5) 并试图找出在哪里使用 onEnter 钩子(Hook)以及在哪里 < strong>onStart:

$transitions.onStart()

$transitions.onEnter()

在它只是事件之前 $stateChangeStart

最佳答案

这是如何the doc描述了转换生命周期钩子(Hook)的执行顺序:

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

...但它似乎有点过时了(我稍后再谈)。不过,它清楚地表明 onEnter Hook 是关于进入状态,而 onStart Hook 是关于开始状态之间的转换 em>.

实际上,the hook doc pages 中对该关键区别进行了很好的描述:

onStart hooks are invoked asynchronously, in priority order, when the Transition starts running. At this point, the Transition has not exited nor entered any states yet.

onEnter hooks are invoked asynchronously, in priority order, when the Transition is entering a state. States are entered after the onRetain hooks.

onStart Hook 似乎是验证转换的好地方 - 例如,检查用户是否经过身份验证。这是该文档中给出的代码示例:

$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)

注意:除了最后两个钩子(Hook)之外的所有钩子(Hook)都可以修改转换 - 更改目标状态等。onSuccessonError 在事后触发:转换结束。

关于javascript - 用户界面路由器 : Difference between onEnter and onStart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38496914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com