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