解决Vue中同一路由报错

路由跳转问题

在这里插入图片描述

Js处理版本

import VueRouter from 'vue-router';
const oldPush = VueRouter.prototype.push;
const oldReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (location) {
  return oldPush.call(this, location).catch(err => err);
};
VueRouter.prototype.replace = function (location) {
  return oldReplace.call(this, location).catch(err => err);
};

Ts处理版本

import VueRouter, { RawLocation, Route } from 'vue-router';
const oldPush: (location: RawLocation) => Promise<Route> = VueRouter.prototype.push;
const oldReplace: (location: RawLocation) => Promise<Route> = VueRouter.prototype.replace;
VueRouter.prototype.push = function (location: RawLocation) {
  return oldPush.call(this, location).catch((err: any) => err);
};
VueRouter.prototype.replace = function (location: RawLocation) {
  return oldReplace.call(this, location).catch((err: any) => err);
};

版权声明:本文为qq_34191778原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。