将代码补充到router路由文件里
// 路由守卫
router.beforeEach((to, from, next) => {
// 判断本地有没有token
const token = getToken()
if (to.path !== '/login' && token === null) {
next('/login')// 终端路由跳转,强制修改成/login 意思:强制切换页面到登录页
} else {
next()
}
})
权限 ---> 如果登录了,本地有token,就无法跳转到登录页面
import router from './index' // 路由文件
import { getToken } from '@/utils/token' // token文件
router.beforeEach((to, from, next) => {
// to:要去的路由路径相关信息对象
// from : 要离开的路由页面,相关的路由信息对象
// next:next()就方形,next(false)原地呆着, next('/地址') 终端跳转直接切换到指定的路由路径
if (getToken() !== null && to.path === '/login') {
next(false)
} else {
next()
}
})