解决编程式路由导航多次点击出错

具体场景:在Vue中使用编程式导航多次点击出错,Vue路由跳转报错Avoided redundant navigation to current location: “/xxxxxx“.
原因和解决方法如下:
编程式导航多次点击路由时报错的原因是因为
vue-router在3.1.0版本之后,push和replace方法会返回一个promise对象,如果不捕获异常,控制台会报错

解决方法:在router/index.js中加入以下代码(可能每个人的路由配置文件不同,下面放一下我的)
代码注释里面有非常详细的原因和解释说明

// 下面是解决编程式导航多次点击路由时报错的方法'
//编程式导航多次点击路由时报错的原因是因为
//vue-router在3.1.0版本之后,push和replace方法会返回一个promise对象,如果不捕获异常,控制台会报错
//解决方法:在router/index.js中加入以下代码
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;

VueRouter.prototype.push = function push(location) {
    // 解释一下下面代码
    // 1.首先调用原来的push方法,将location传进去
    // 2.然后调用catch方法,捕获异常
    // 3.最后返回一个promise对象
    return originalPush.call(this, location).catch(err => err);
};
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};

我的index.js文件如下

import VueRouter from "vue-router";

import Home from "../components/Home.vue";
import Course from "../components/Course.vue";
import Front from "../components/Front.vue";
import Back from "../components/Back.vue";
const routes = new VueRouter({
    routes: [
        {
            path: "/home1",
            // 下面这行代码的意思是:当访问 /home 路径时,加载 Home 组件
            // component: () => import("../components/Home.vue"),
            component:Home,
        },
        {
            path: "/course",
            component:Course,
            children:[
                {
                    path:"front",
                    component:Front,
                },
                {
                    // 如果使用params传参,参数为对象的形式,那么必须要在路由这里单独加name属性,可以不加path后面的text参数
                    name:'houduan',
                    path:"back/:text",
                    component:Back,
                },
            ]
        },
    ],
});
// 下面是解决编程式导航多次点击路由时报错的方法'
//编程式导航多次点击路由时报错的原因是因为
//vue-router在3.1.0版本之后,push和replace方法会返回一个promise对象,如果不捕获异常,控制台会报错
//解决方法:在router/index.js中加入以下代码
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;

VueRouter.prototype.push = function push(location) {
    // 解释一下下面代码
    // 1.首先调用原来的push方法,将location传进去
    // 2.然后调用catch方法,捕获异常
    // 3.最后返回一个promise对象
    return originalPush.call(this, location).catch(err => err);
};
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};
export default routes;

我的调用代码如下

methods: {
    toFront() {
      // this.$router.push({path:'/course/front',query:{text:this.text}})
      // this.$router.push({ name: "houduan", params: { text: this.text } });
      this.$router.replace({ name: "houduan", params: { text: this.text } });
    },
  },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值