router.js
export default new Router({
routes: [{
path: '/login',
component: Login,
meta: {
requireAuth: false // 不需要鉴权
}
}, {
path: "/home",
component: Home,
meta: {},
}}]
})
main.js
router.beforeEach((to, from, next) => {
const type = to.meta.type
// 判断该路由是否需要登录权限
if (to.meta.requireAuth) {
// 该路由需要登录权限
if (sessionStorage.getItem('token')) {
// 已登录
next()
} else {
next('/login')
}
} else {
next()
}
})