Uncaught (in promise) undefined


Vue-router >= 3.1.0 版本在使用 push 和 replace 進行跳轉時控制台會拋出異常,其主要原因是 vue-router 3.1.0 版本以后 router.push('/path') 返回了 promise ,而當路由跳轉異常時便會拋出錯誤,此前版本沒有報錯是因為 vue-router 根本沒有返回錯誤信息,所以之前我們一直無法捕獲異常,而並非異常不存在。當然,很多時候這些報錯我們不用關心,除非有些地方需要特殊處理。下面給出些解決方法。

使用時進行錯誤攔截:

router.push('/path').catch(err => {})

全局進行錯誤攔截:

const routerMethods = ['push', 'replace']
routerMethods.forEach(method => {
  const originalCall = VueRouter.prototype[method]
  VueRouter.prototype[method] = function(location, onResolve, onReject) {
    if (onResolve || onReject) {
      return originalCall.call(this, location, onResolve, onReject)
    }
    return originalCall.call(this, location).catch(err => err)
  }
})

參考

https://github.com/vuejs/vue-router/issues/2881


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM