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


1、報錯原因

在升級了Vue-Router版本到到3.1.0及以上之后,頁面在跳轉路由控制台會報Uncaught (in promise)的問題。

這是什么原因呢?

看vue-router的版本更新日志

V3.1.0版本里面新增功能:push和replace方法會返回一個promise, 你可能在控制台看到未捕獲的異常。

 2、解決方法

方法一:在調用方法的時候用catch捕獲異常

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

方法二:對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刪除。



猜您在找 vue 請求報錯 Uncaught (in promise) 的解決方法 vue報錯 Uncaught (in promise) NavigationDuplicated {_name:""NavigationDuplicated"... 的解決方法 vue-router 3.1.5報錯:vue-router.esm.js?8c4f:2089 Uncaught (in promise) 解決Vue-Router升級導致的Uncaught(in promise) navigation guard問題 VUE.js項目中控制台報錯: Uncaught (in promise) NavigationDuplicated解決方法 Vue. 之 報錯 Uncaught (in promise) vue-router(hash模式)常見問題以及解決方法 Vue報錯:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解決方法 Vue報錯:Uncaught TypeError: Cannot assign to read only property 'exports' of object 的解決方法 vue報錯vue-router.esm.js?8c4f:2007 Uncaught (in promise) NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}
 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM