为什么beforeRouteEnter
vue-router中存在导航保护?是否存在beforeRouteEnter
会被解雇但mounted
不会解雇的情况?如果不是,在什么情况下,你会更喜欢使用beforeRouteEnter
到mounted
?
这mounted
是任何Vue组件的生命周期挂钩,它将始终被触发。beforeRouteEnter
或添加的任何其他生命周期挂钩的想法vue-router
是允许您控制应用程序。
例如,假设您有一个名为的路由bar
,该路由具有真正特定的验证逻辑,如果先前的路由为foo
,则仅允许用户输入该验证逻辑,您可以将该验证逻辑插入此挂钩中,而不用检查路由中的每个路由更改全球卫队。
export default {
name: 'Bar',
beforeRouteEnter(to, from, next) {
if (from.name === 'foo') {
next(); // Calling next allow the route to proceed
} else {
next(false); // Don't allow the navigation
// or
next({
name: 'foo',
query: {
from: 'bar'
}
}); // Redirect to any desired route as navigation made in $router
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句