//main.js
router.beforeEach(function (to, from, next) {
if(to.meta.requireAuth){
// 要去的url只有登陆成功后才能访问
if (store.state.username) {
next()
} else {
next({name: 'login'})
}
}else{
next()
}
})
//routers.js
routes: [
{
path: '/micro',
name: 'micro',
component: Micro,
meta:{
requireAuth:true
}
},
{
path: '/news',
name: 'news',
component: News,
meta:{
requireAuth:true
}
},
{
path: '/login',
name: 'login',
component: Login
}
],
RE: Vue.js实战笔记