vue跳轉(zhuǎn)同一路由報(bào)錯(cuò)的問(wèn)題及解決
vue跳轉(zhuǎn)同一路由報(bào)錯(cuò)
vue中,如果跳轉(zhuǎn)同一個(gè)頁(yè)面路由,雖不會(huì)影響功能,但是會(huì)報(bào)錯(cuò)

原因:路由的push會(huì)向歷史記錄棧中添加一個(gè)記錄,同時(shí)跳轉(zhuǎn)同一個(gè)路由頁(yè)面,會(huì)造成一個(gè)重復(fù)的添加,導(dǎo)致頁(yè)面的報(bào)錯(cuò)
解決方案:在router的index.js中重寫(xiě)vue的路由跳轉(zhuǎn)push
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err);
}編程式路由跳轉(zhuǎn)多次點(diǎn)擊報(bào)錯(cuò)問(wèn)題
使用編程式路由進(jìn)行跳轉(zhuǎn)時(shí),控制臺(tái)報(bào)錯(cuò),如下所示。

問(wèn)題分析
該問(wèn)題存在于Vue-router v3.0之后的版本,由于新加入的同一路徑跳轉(zhuǎn)錯(cuò)誤異常功能導(dǎo)致。
解決方法
重寫(xiě) $router.push 和 $router.replace 方法,添加異常處理。
//push
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
return VueRouterPush.call(this, to).catch(err => err)
}
//replace
const VueRouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (to) {
return VueRouterReplace.call(this, to).catch(err => err)
}
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 編程式路由跳轉(zhuǎn)多次點(diǎn)擊報(bào)錯(cuò)問(wèn)題</title>
</head>
<body>
<div id="app">
<button @click="pageFirst">Page First</button>
<button @click="pageSecond">Page Second</button>
<router-view></router-view>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script>
const First = { template: '<div>First Page</div>' } //調(diào)用路由name屬性
const Second = { template: '<div>Second Page</div>' }
routes = [
{ path:'/first', name:"first" ,component: First }, //設(shè)置路由name屬性
{ path: '/second', name:"second", component: Second }
]
router = new VueRouter({
routes
})
//push
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
return VueRouterPush.call(this, to).catch(err => err)
}
//replace
const VueRouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (to) {
return VueRouterReplace.call(this, to).catch(err => err)
}
const app = new Vue({
router,
methods: {
pageFirst(){ router.push('/first') },
pageSecond(){ router.push({ name: 'second' }) },
},
}).$mount('#app')
</script>
</body>
</html>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一步步帶你用vite簡(jiǎn)單搭建ts+vue3全家桶
Vue3與TS的聯(lián)合是大趨勢(shì),下面這篇文章主要給大家介紹了關(guān)于用vite簡(jiǎn)單搭建ts+vue3全家桶的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
vue3容器布局和導(dǎo)航路由實(shí)現(xiàn)示例
這篇文章主要為大家介紹了vue3容器布局和導(dǎo)航路由實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
vue-quill-editor 自定義工具欄和自定義圖片上傳路徑操作
這篇文章主要介紹了vue-quill-editor 自定義工具欄和自定義圖片上傳路徑操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
詳解vue-router數(shù)據(jù)加載與緩存使用總結(jié)
這篇文章主要介紹了詳解vue-router數(shù)據(jù)加載與緩存使用總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
vue項(xiàng)目中極驗(yàn)驗(yàn)證的使用代碼示例
這篇文章主要介紹了vue項(xiàng)目中極驗(yàn)驗(yàn)證的使用代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue如何實(shí)現(xiàn)本項(xiàng)目頁(yè)面之間跳轉(zhuǎn)
這篇文章主要介紹了vue如何實(shí)現(xiàn)本項(xiàng)目頁(yè)面之間跳轉(zhuǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vuex 多模塊時(shí) 模塊內(nèi)部的mutation和action的調(diào)用方式
這篇文章主要介紹了vuex 多模塊時(shí) 模塊內(nèi)部的mutation和action的調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue實(shí)現(xiàn)搖一搖功能(兼容ios13.3以上)
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)搖一搖功能,兼容ios13.3以上,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01

