路由跳转到相同地址或其他原因抛出异常时,可以在router配置文件中,写上
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
return routerPush.call(this, location).catch(error => error);
};
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
return originalReplace.call(this, location).catch(err => err);
}
合并写法
const routerMethods = ['push', 'replace'];
routerMethods.forEach(method => {
const originalCall = router.__proto__[method]
router.__proto__[method] = function(location, onResolve, onReject) {
if(onResolve || obReject) {
return originalCall.call(this, location, onResolve, onReject);
}
return originalCall.call(this, location).catch(err => err)
}
})