1. 程式人生 > 程式設計 >Vue-router 報錯NavigationDuplicated的解決方法

Vue-router 報錯NavigationDuplicated的解決方法

版本:3.1.x

Vue-router 報錯NavigationDuplicated的解決方法

報錯原因:

使用push()、replace()進行導航時,不能重複導航到當前路由。

解決辦法:

方法1:在定義路由的檔案中router/index.js

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
 return originalPush.call(this,location).catch(err => err)
}

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.repalce = function replace (location) { 
 return originalReplace.call(this,location).catch(err => err)
}

方法2:在呼叫push()、replace()方法時,catch

this.$router
  .replace(this.path)
  .catch(err => err) 

說明:第一種方法好像對replace()沒有作用。

到此這篇關於Vue-router 報錯NavigationDuplicated的解決方法的文章就介紹到這了,更多相關Vue-router 報錯NavigationDuplicated內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!