代码如下:
new Vue({
el: '#app',
render: h => h(App),
router,
store,
template: '<App/>',
components: { App },
})
router.beforeEach((to, from, next) => {
if(to.meta.requireAuth) { //如果下一个页面的requireAuth是true,则验证token是否存在
console.log(store.state.token)
if(store.state.token){
next();
}else{
next({path: '/login'})
}
}else{
next();
}
})
错误原因: router.beforeEach 放在代码后面了
修正之后:
router.beforeEach((to, from, next) => {
if(to.meta.requireAuth) { //如果下一个页面的requireAuth是true,则验证token是否存在
console.log(store.state.token)
if(store.state.token){
next();
}else{
next({path: '/login'})
}
}else{
next();
}
})
new Vue({
el: '#app',
render: h => h(App),
router,
store,
template: '<App/>',
components: { App },
})
代码如下:
错误原因: router.beforeEach 放在代码后面了
修正之后: