vue实现导航守卫——router.beforeEach
2020-07-29 本文已影响0人
前端阿峰
在main.js文件中进行配置:
// 这里是路由守卫, 路由跳转前的处理
router.beforeEach((to, from, next) => {
// to 跳到哪里 from 从哪里
let token = Cookies.get("token")
// 判断是否登录
if (!token) { // 未登录,跳转登录页
window.location.href = config.loginURL
} else if (token == "undefined") {
window.location.href = config.loginURL
} else {
// to.meta 是路由里面的meta
if (to.meta.auth) {
// auth=true 跳转
next()
} else {
// auth = false 跳转到not
next({ path: '/not' })
}
}
})