网页一进入判断是否登陆,未登陆跳转到登陆页面code
export default new Router({ routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld, meta: { title: '首页', type: 'login' // 是否须要判断是否登陆,这里是须要判断 } }, { path: '/login', name: 'login', component: login, meta: { title: 'login', type: '' // 不须要鉴权 } } ] })
router.beforeEach((to, from, next) => { if (to.meta.title) { document.title = to.meta.title } const type = to.meta.type // 判断该路由是否须要登陆权限 if (type === 'login') { if (window.localStorage.getItem('login')) { next() } else { next('/login') } } else { next() // 确保必定要有next()被调用 } })