修改vue.prototype.push方法,解决跳转相同路径的报错。

103 阅读1分钟

在vue-router在3.1.0版本之后,push和replace方法会返回一个promise对象,如果跳转到相同的路由,就报promise uncaught异常.

我们可以在router文件中将push,replace方法重写,代码如下:

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