vue-router報錯Uncaught (in promise)及解決方法


1、報錯原因:

 在升級了Vue-Router版本到到3.1.0及以上之后,頁面在跳轉路由控制台會報Uncaught (in promise)的問題,在3.1.0版本里面新增功能:push和replace方法會返回一個promise, 你可能在控制台看到未捕獲的異常。

2、解決方法:

a、在調用方法的時候用catch捕獲異常

this.$router.replace('/home').catch(err => {
   console.log(err)
})

  

b、對Router原型鏈上的push、replace方法進行重寫,這樣就不用每次調用方法都要加上catch

在router.js加入以下內容:

import Router from 'vue-router'
 
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)
}

  


免責聲明!

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



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