報(bào)錯(cuò)信息
vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/home".
?
報(bào)錯(cuò)原因:重復(fù)點(diǎn)擊路由導(dǎo)致,因?yàn)関ue-router引入了promise,當(dāng)我們使用this.$router.push時(shí)候需要多添加成功或失敗的回調(diào),否則就會報(bào)出以上的錯(cuò)誤。
原代碼
?解決辦法
第一種
? ? ? ? 在進(jìn)行路由跳轉(zhuǎn)時(shí)對路徑進(jìn)行判斷如果重復(fù)即不再執(zhí)行
????????
switchTab(path) {
if (this.$router.path == path) return
this.$router.push(path)
}
第二種
? ? ? ? 跳轉(zhuǎn)后使用catch語句對錯(cuò)誤不再進(jìn)行處理
this.$router.push(path).catch(err => {})
第三種
? ? ? ? 基于第二種方法,當(dāng)我們的路由跳轉(zhuǎn)過多時(shí)就需要每次跳轉(zhuǎn)都要加上catch回調(diào),這樣比較麻煩,所以還可以在引入vueRouter文件下重寫push和replace方法
import VueRouter from 'vue-router';
//保存原型對象的Push
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
//重寫push方法
VueRouter.prototype.push = function (location, res, rej) {
if (res && rej) {
originPush.call(this, location, res, rej)
}
else {
originPush.call(this, location, () => { }, () => { })
}
}
//重寫replace方法
VueRouter.prototype.replace = function (location, res, rej) {
if (res && rej) {
originReplace.call(this, location, res, rej)
}
else {
originReplace.call(this, location, () => { }, () => { })
}
}
csdn文章推薦受影響解決辦法10個(gè)字10行文章來源地址http://www.zghlxwxcb.cn/news/detail-403836.html
csdn文章推薦受影響解決辦法10個(gè)字10行文章來源:http://www.zghlxwxcb.cn/news/detail-403836.html
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
csdn文章推薦受影響解決辦法10個(gè)字10行
到了這里,關(guān)于vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!