之前有看过一个写法
// const originalPush = Router.prototype.push
// Router.prototype.push = function push(location, onResolve, onReject) {
// if (onResolve || onReject)
// return originalPush.call(this, location, onResolve, onReject)
// return originalPush.call(this, location).catch((err) => err)
// }
这个写法发现如果改变路由模式 mode 改history 会出问题。
原因:
permission.js里的next({ ...to, replace: true })会被认为是一个失败的navigation(虽然能导航成功,但不再是原来的to),所以login里的push()返回一个rejected Promise。
解决方案:在push()后面接一个.catch(()=>{})
// 避免eslint报错需要添加 eslint-disable-line
this.$router.push({ path: '/' }).catch(() => {})// eslint-disable-line