码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 熟悉Vue路由的beforeEach陷入死循环的情况


    Vue的全局路由分为全局前置路由与全局后置路由

    官方对前置路由守卫的介绍

    在这里插入图片描述
    不了解路由守卫的next很容易让页面陷入死循环。

    以全局前置路由为例,陷入路由死循环一般报错是堆栈溢出也就是:RangeError: Maximum call stack size exceeded

    router.js

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    
    Vue.use(VueRouter)
    
    const routes = [
      {
        path:"/",
        redirect:"/Index"
      },
      {
        path:"/Index",
        component:()=>import('@/views/Index.vue')
      },
      {
        path:"/Center",
        component:()=>import('@/views/Center.vue')
      },
      {
        path:"/an",
        component:()=>import('@/views/an.vue')
      }
    ]
    
    const router = new VueRouter({
      routes
    })
    
    router.beforeEach((to,from,next)=>{
      console.log("to==>",to);
      console.log("from==>",from);
      if(from.path=='/Index'){
        next('/an')
      }else{
        next();
      }
    })
    
    export default router
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    当从Index.vue离开时就会陷入死循环。

    在这里插入图片描述
    要清楚前置路由陷入死循环的原因,就要明白前置路由守卫的next函数

    next函数与beforeEach前置守卫是互相调用的有个过程。
    next(‘/’)执行完成后就会执行beforeEach路由守卫

    router.beforeEach((to,from,next)=>{
      console.log("to==>",to.path);
      console.log("from==>",from.path);
      if(from.path=='/Index'){
        next('/an')
      }else{
        next();
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    流程解析:刚开始的时候 目标路由是/Center,来源路由是/Index,执行前置路由守卫,判断到if为true,于是执行next(‘/an’),目标路由改成了/an,但来源路由仍然是/Index,next执行完成后再次执行beforeEach,beforeEach的判断再次为true,再次执行next(‘/an’),目标路由改成/an,来源路由仍然没变,因为next执行完成后再次执行beforeEach,于是就陷入了死循环。


    如果要判断to.path陷入死循环,只需要to.path的值等于next的入参。
    router.beforeEach((to,from,next)=>{
      console.log("to==>",to.path);
      console.log("from==>",from.path);
      if(to.path=='/Index'){
        next('/Index')
      }else{
        next();
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    Java—基础知识测试总结1
    为什么解决文档管理问题的数字化战略能够推动盈利增长
    【LangChain学习之旅】—(11) 记忆:通过Memory记住用户上次的对话细节
    华为OD机试真题 Java 实现【矩阵元素的边界值】【2023 B卷 100分】,附详细解题思路
    Python面试1-50题汇总,都是常问的面试题!
    Java学习路线图
    前端下载文件重命名
    springboot中如何同时操作同一功能
    BPF 调度器 sched_ext 实现机制、调度流程及样例
    [附源码]计算机毕业设计基于springboot架构的博客平台设计
  • 原文地址:https://blog.csdn.net/qq_44849271/article/details/125565601
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号