美文网首页
解决 Uncaught (in promise) Error:

解决 Uncaught (in promise) Error:

作者: 上海_前端_求内推 | 来源:发表于2022-09-21 17:44 被阅读0次

这个错误是vue-router内部错误,没有进行catch处理,导致的编程式导航跳转问题,往同一地址跳转时会报错的情况

push和replace 都会导致这个情况的发生

解决方法如下:在路由器中进行配置

import VueRouter from 'vue-router';
Vue.use(VueRouter);
//解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
//push
VueRouter.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)
}
//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
    return originalReplace.call(this, location).catch(err => err)
}

相关文章

网友评论

      本文标题:解决 Uncaught (in promise) Error:

      本文链接:https://www.haomeiwen.com/subject/fyawortx.html