解决Vue路由重复点击报错问题

给一个元素绑定跳转路由的事件时,跳转后当我们重复点击时就会报以下错误:

  原因:vueRouter版本问题

解决方案1:

降低VueRouter的版本  使用旧版本的VueRouter   3.0.xxx

 解决方案2:

跳转路径后,捕获异常,不做处理(这类绑定事件若较多,要写好多次catch)

$router.push('/home/search').catch(err=>{ })

 解决方案3(也是推荐的方法):

const routerPush = VueRouter.prototype.push;

VueRouter.prototype.push = function (location) {

    return routerPush.call(this, location).catch(err => {})

};

注意:在VueRouter上配置路由跳转,在router中的index.js中加上以下代码,注意加在use之前写

 完整示例:

import Vue from "vue";
import VueRouter from "vue-router";
// 主页面 布局页面 其他页面都是依托这个Layout
import Layout from "../views/Layout.vue";

const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return routerPush.call(this, location).catch((err) => {});
};

Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", name: "", component: () => import("../views/Login.vue") },
  {
    path: "/home",
    name: "",
    meta: { title: "后台首页" },
    component: Layout,
  },
  {
    path: "/users",
    name: "",
    meta: { title: "用户管理" },
    component: Layout,
    children: [
      { path: "/", redirect: "users" },
      {
        path: "users",
        meta: { title: "用户列表" },
        component: () => import("../views/users/users.vue"),
      },
    ],
  },
  {
    path: "/orders",
    name: "",
    meta: { title: "订单管理" },
    component: Layout,
    children: [
      { path: "/", redirect: "orders" },
      {
        path: "orders",
        meta: { title: "订单列表" },
        component: () => import("../views/orders/orders.vue"),
      },
    ],
  },
  {
    path: "/rights",
    name: "",
    meta: { title: "权限管理" },
    component: Layout,
    children: [
      { path: "/", redirect: "rights" },
      {
        path: "rights",
        meta: { title: "权限列表" },
        component: () => import("../views/rights/rights.vue"),
      },
      {
        path: "roles",
        meta: { title: "角色列表" },
        component: () => import("../views/rights/roles.vue"),
      },
    ],
  },
  {
    path: "/reports",
    name: "",
    meta: { title: "数据统计" },
    component: Layout,
    children: [
      { path: "/", redirect: "reports" },
      {
        path: "reports",
        meta: { title: "数据报表" },
        component: () => import("../views/reports/reports.vue"),
      },
    ],
  },
  {
    path: "/goods",
    name: "",
    meta: { title: "商品管理" },
    component: Layout,
    children: [
      { path: "/", redirect: "goods" },
      {
        path: "goods",
        meta: { title: "商品列表" },
        component: () => import("../views/goods/goods.vue"),
      },
      {
        path: "params",
        meta: { title: "分类参数" },
        component: () => import("../views/goods/params.vue"),
      },
      {
        path: "categories",
        meta: { title: "商品分类" },
        component: () => import("../views/goods/categories.vue"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

export default router;

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值