網頁一進入判斷是否登陸,未登陸跳轉到登陸頁面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()被調用 } })