• vue2、vue3中路由守卫变化


    什么是路由守卫

    路由守卫就是路由跳转的一些验证,比如登录鉴权(没有登录不能进入个人中心页)等等等
    路由守卫分为三大类:

    1. 全局守卫:前置守卫:beforeEach 后置钩子:afterEach

    2. 单个路由守卫:独享守卫:beforeEnter

    3. 组件内部守卫:beforeRouteEnter beforeRouteUpdate beforeRouteLeave
      所有的路由守卫都是三个参数:

      to: 要进入的目标路由(去哪儿)

      from: 要离开的路由(从哪来)

      next: 是否进行下一步(要不要继续

    写next()相当于 next(true) 继续执行

    不写 相当于next(false)终止执行

    next(path)跳转 例如:next("/login")

    注意:后置钩子afterEach没有next参数,我们来详细看看都是怎么使用的叭

    全局守卫:

    全局前置守卫beforEach:

    给需要守卫的路由加上: meta: { permission: true },

    router.beforeEach((to, from, next) => {
      if (to.meta.permission) {
        if (sessionStorage.getItem("token")) {
          next();
        } else {
          alert("请先登录");
          next("/login");
        }
      } else {
        next();
      }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    全局后置钩子afterEach(少用)

    router.afterEach((to, from) => {
      // to and from are both route objects.
    });
    
    • 1
    • 2
    • 3

    单个路由守卫:

      // 首页模块路由
      {
        path: "/index",
        name: "index",
        meta: { permission: true },
        component: () => import("../views/Index.vue"),
        beforeEnter: function(to, from, next) {
          if (sessionStorage.getItem("token")) {
            next();
          } else {
            alert("请先登录");
            next("/login");
          }
        }
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    组件内部守卫:

    datacreatedmountedmethods处于平等关系

    beforeRouteEnter举例说明:

      beforeRouteEnter(to, from, next) {
        // 在渲染该组件的对应路由被 confirm 前调用
        // 不!能!获取组件实例 `this`
        // 因为当守卫执行前,组件实例还没被创建
        if (sessionStorage.getItem("token")) {
          next();
        } else {
          alert("请先登录");
          next("/login");
        }
      },
      beforeRouteUpdate(to, from, next) {
        // 在当前路由改变,但是该组件被复用时调用
        // 可以访问组件实例 `this`
      },
      beforeRouteLeave(to, from, next) {
        // 导航离开该组件的对应路由时调用
        // 可以访问组件实例 `this`
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    beforeRouteEnter 是进入前

    beforeRouteUpdate 是路由变化时

    beforeRouteLeave 是离开后。这个离开守卫通常用来禁止用户在还未保存修改前突然离开。该导航可以通过 next(false) 来取消。

    注意:
    beforeRouteEnter 守卫 不能 访问 this,因为守卫在导航确认前被调用,因此即将登场的新组件还没被创建。

    vue2、vue3中路由守卫变化

    router.beforeEach((to,from)=>{
        // 现在next可选
        
        // vue2 :
        // 写next()相当于 next(true) 继续执行
        // 不写 相当于next(false)终止执行
        // next(path) 跳转指定路由(重定向) 
        //--------------------------------
        // vue3 :
        // 返回true | undefined 放行
        // 返回false取消
        // 返回路由地址(对象格式) 重定向
        
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    例子:

    // 访问权限控制
    router.beforeEach((to) => {
      // 用户仓库
      const store = useUserStore()
      // 不需要登录的页面,白名单
      const wihteList = ['/login']
      // 如果没有登录(pinia中没有token)且不在白名单内,去登录
      if (!store.user?.token && !wihteList.includes(to.path)) return '/login'
      // 否则不做任何处理,什么都不返回则默认返回undefined
    })
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    Netty网络框架学习笔记-18(NioEventLoop源码与处理器异步任务分析)
    1024程序员节——我是猿,我为自己带盐
    人工神经网络的应用价值,人工智能神经网络应用
    Nginx代理解决CORS跨域
    多线程-定时器、线程池
    前端开发:JS中字符串拼接的总结
    用DIV+CSS技术设计的环保主题网站(web前端网页制作课作业)
    pandas学习资源
    自动化报表实践小结
    知识点链接总结
  • 原文地址:https://blog.csdn.net/qq_40589140/article/details/134045033