vue2路由跳转是异步的

在 Vue 2 中,如果你在路由跳转函数中通过路由路径判断路径时,发现路径还是上一个路径,这是因为路由跳转是异步的。为了确保在路由跳转完成后进行判断,你可以使用路由的导航守卫或者 nextTick 来确保获取到最新的路由路径。

使用 $nextTick

使用 $nextTick 确保在 DOM 更新后进行判断:

methods: {
  navigateAndCheck() {
    this.$router.push('/new-path').then(() => {
      this.$nextTick(() => {
        if (this.$route.path === '/new-path') {
          console.log('Successfully navigated to new path');
          // 继续你的逻辑
        }
      });
    });
  }
}

相关推荐

  1. vue2异步

    2024-07-18 14:04:02       21 阅读
  2. vue2 / vue3 (返回&)时判断 + 取消

    2024-07-18 14:04:02       38 阅读
  3. vue3

    2024-07-18 14:04:02       39 阅读
  4. uniapp方式

    2024-07-18 14:04:02       38 阅读
  5. 如果用户访问没有页面到404

    2024-07-18 14:04:02       20 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 14:04:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 14:04:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 14:04:02       58 阅读
  4. Python语言-面向对象

    2024-07-18 14:04:02       69 阅读

热门阅读

  1. 日有所增,不见其长

    2024-07-18 14:04:02       20 阅读
  2. Python面试整理-Python的数据类型,分别有哪些?

    2024-07-18 14:04:02       20 阅读
  3. WordPress与 wp-cron.php

    2024-07-18 14:04:02       15 阅读
  4. LeetCode //C - 231. Power of Two

    2024-07-18 14:04:02       21 阅读
  5. Leetcode617. 两个二叉树相加

    2024-07-18 14:04:02       16 阅读
  6. request method ‘DELETE‘ is not supported问题

    2024-07-18 14:04:02       21 阅读
  7. 【日常技能】excel 换行符替换的3个方法完美解决

    2024-07-18 14:04:02       21 阅读
  8. C# —— Sort排序

    2024-07-18 14:04:02       24 阅读
  9. centos跳过首次创建用户

    2024-07-18 14:04:02       21 阅读
  10. 使用Spring Retry实现重试机制

    2024-07-18 14:04:02       21 阅读
  11. 一行命令实现 Github 国内下载加速

    2024-07-18 14:04:02       22 阅读
  12. kotlin 退出Activity 平滑动画

    2024-07-18 14:04:02       21 阅读