美文网首页
vue实现导航守卫——router.beforeEach

vue实现导航守卫——router.beforeEach

作者: 前端阿峰 | 来源:发表于2020-07-29 17:41 被阅读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' })
        }
    }
})

相关文章

网友评论

      本文标题:vue实现导航守卫——router.beforeEach

      本文链接:https://www.haomeiwen.com/subject/omckrktx.html