解決"Uncaught (in promise) Error: Navigation cancelled from "/" to "/login" with a new navigation"報錯處理


 Uncaught (in promise) Error: Navigation cancelled from “/” to “/login” with a new navigation.

這個錯誤是vue-router內部錯誤,沒有進行catch處理,導致的編程式導航跳轉問題,往同一地址跳轉時會報錯的情況。

push和replace 都會導致這個情況的發生。

 

解決方法如下:

在路由器中進行配置:router/index.js

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
// 解決報錯
const originalPush = Router.prototype.push
const originalReplace = Router.prototype.replace
// 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)
}
// replace
Router.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)
}

 

僅在此記錄一下遇到的問題和最后的解決辦法,親測可用。

 


免責聲明!

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



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