某些页面需要验证是否登录,如果登录就可以访问,否则跳转到登陆页面
Vue.use(VueRouter);
const Routers = [
{
path:'/index',
component:(resolve) =>require(['./views/index.vue'],resolve)
},
]
const RoutersConfig = {
mode:'history', //开启HTML5的History路由模式
routes:Routers
};
const router = new VueRouter(RoutersConfig);
router.beforeEach((to,from,next) => { //vuerouter提供的导航钩子beforeEach和afterEach,在路由改变前或者改变后出发
if(window.localStorage.getItem('token')){
next();//可以进入下一个路由
}esle{
next('/login');//回到登陆页面 如果是next(false)就是取消导航
}
})