【发布时间】:2019-11-21 09:17:50
【问题描述】:
我使用的是 vue 2.6.10,在我的 vue 的 router.js 中我设置了一些类似这样的路由
Vue.use(Router)
const router = new Router({
mode: "history",
base: process.env.BASE_URL,
routes: [
{
path: '/login',
name: 'login',
component: Login,
},
{
path: '/shoes',
name: 'shoes',
component: Shoes,
beforeEnter: (to, from, next) => {
if (store.state.login.quad_token == null) {
next('/login');
}
next();
},
children:[
{
path: ':skateshoes',
name: 'skateshoes',
component: SkateShoes
},
//more children routes
问题是,如果我从浏览器中手动删除 cookie 并到达 /shoes/skateshoes,我不会被重定向到登录页面。
要获得重定向,我必须像这样编辑 skateshoes 子路由
{
path: ':skateshoes',
name: 'skateshoes',
component: SkateShoes,
beforeEnter: (to, from, next) => {
if (store.state.login.quad_token == null) {
next('/login');
}
next();
}
},
我认为将beforeEnter 放在父母中也适用于所有孩子。显然这不起作用,至少对我来说。
如何让它发挥作用?谢谢
【问题讨论】:
标签: vue.js authentication redirect vue-router