Vue Router路由無法跳轉(zhuǎn)問題匯總
問題集
整理了部分Vue Router路由無法跳轉(zhuǎn)問題:
- 頂層
router-view
只能被頂層路由配置內(nèi)容使用:此問題異常表現(xiàn)在路由跳轉(zhuǎn)但頁面不變
- 子路由跳轉(zhuǎn)必需父路由對(duì)應(yīng)的組件中存在
router-view
:此問題異常表現(xiàn)在路由跳轉(zhuǎn)但頁面不變
- 子路由配置路徑會(huì)自動(dòng)繼承父路徑并自動(dòng)增加
/
- 如果子路徑配置路徑存在前綴
/
,則代表為全路徑,需要包含父路由路徑
- 子路由配置路徑會(huì)自動(dòng)繼承父路徑并自動(dòng)增加
- 跳轉(zhuǎn)路由與當(dāng)前路由相同時(shí),即
重復(fù)路由
時(shí),會(huì)觸發(fā)NavigationDuplicated
錯(cuò)誤
頂層路由視圖只能頂層配置使用
<!-- App.vue --> <template> <div id="app"> <router-view/> <!--頂層路由視圖--> </div> </template>
父組件
子層路由視圖,只能子路由配置可以使用,比如 /parent
路由中的children子路由配置 child
換句話說:子路由可以跳轉(zhuǎn)則必需對(duì)應(yīng)父路由的組件中村啊在 <router-view/>
<!-- Parent.vue --> <template> <div id="parent"> Parent Content <router-view/> <!--關(guān)鍵點(diǎn):子層路由視圖--> </div> </template>
子組件
<!-- Child.vue --> <template> <div id="child"> Child Content </div> </template>
路由配置
一級(jí)路由只能用在在頂層路由視圖,如 name=Home,Parent
等路由只能用在 App.vue
中的 <router-view/>
- 子路由只能用在父組件中的
<router-view/>
中,如name=Child
路由只能用在Parent.vue
中的<router-view/>
- 如果Child組件中仍有
<router-view/>
,并且需要使用,- 則可以在
name=Child
路由中繼續(xù)配置children
子路由 - 或者配置新的和’name=Parent’同級(jí)別的路由配置依賴
Child.vue
組件,如name=NewChild
,并在其中配置children
子路由
- 則可以在
// router配置 export default new Router({ routes: [ { path: '/', name: 'Home', redirect:'/parent', // 可以使用App.vue中的頂層路由視圖 }, { path: '/parent', name: 'Parent', component: Parent, children: [ { path: 'child', // 非全路徑配置時(shí),子路徑開頭無需`/`,只需子路徑`child` // path : '/parent/child', // 也可以配置全路徑 name: 'Child', component: Child, // children: [] // 如果Child組件中仍有<router-view/>,則可以繼續(xù)配置子路由 }, ], }, { path: '/new-child', name: 'NewChild', component: Child, // children: [] // 如果Child組件中仍有<router-view/>,則可以繼續(xù)配置子路由 }, ], })
子路由路徑問題
- 子路由路徑以
/
開頭代表全路徑配置,需要包含父路由路徑,如path:'/parent/child'
- 子路由可省略
/
開頭,自動(dòng)繼承父路由路徑,如path:'child'
上面也有代碼說明也有介紹
// router配置 export default new Router({ routes: [ { path: '/parent', name: 'Parent', component: Parent, children: [ { path: 'child', // 非全路徑配置時(shí),子路徑開頭無需`/`,只需子路徑`child` // path : '/parent/child', // 也可以配置全路徑 name: 'Child', component: Child, }, ], }, ], })
路由重復(fù)問題
vue-router 不知道哪個(gè)版本開始的問題,小編沒關(guān)心過這個(gè),解決內(nèi)容可以參考.
當(dāng)準(zhǔn)備跳轉(zhuǎn)的路由是當(dāng)前路由是,即假如當(dāng)前路由時(shí) /parent
,仍舊執(zhí)行 this.$router.push('/parent')
就會(huì)報(bào)類似以下錯(cuò)誤:
NavigationDuplicated: Avoided redundant navigation to current location: "/data-manage". at createRouterError (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2053:15) at createNavigationDuplicatedError (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2023:15) at HTML5History.confirmTransition (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2340:18) at HTML5History.transitionTo (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2267:8) at HTML5History.push (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2613:10) at eval (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:3043:24) at new Promise (<anonymous>) at VueRouter.push (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:3042:12) at VueComponent.goto (webpack-internal:///./node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/commons/layout/SideBar.vue:24:26) at click (webpack-internal:///./node_modules/vue-loader/lib/template-compiler/index.js?{"id":"data-v-3cb2454e","hasScoped":false,"transformToRequire":{"video":["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0!./src/components/commons/layout/SideBar.vue:17:30)
解決方法:
- 重寫
vue-router
的push
方法 - 捕獲異常并忽略:當(dāng)然也需要自己定義一個(gè)統(tǒng)一的push方法用來替換使用
this.$router.push
- 自記錄當(dāng)前路徑和要跳轉(zhuǎn)的路徑,如果當(dāng)前路徑和要跳轉(zhuǎn)的路徑相同,則不跳轉(zhuǎn) 重寫push方法
在 router/index.js
(在自己項(xiàng)目的路由配置中哈,不要非要較真~)重寫 VueRouter.push
方法
import VueRouter from 'vue-router' const VueRouterPush = VueRouter.prototype.push VueRouter.prototype.push = function push(to) { return VueRouterPush.call(this, to).catch(err => err) }
捕獲異常并忽略
this.$router.push(url).catch(() => {})
推薦提取為統(tǒng)一公共方法,如:
export const routerPush = (url) => {this.$router.push(url).catch(() => {})}
判斷路徑是否為當(dāng)前路徑
如果路徑非當(dāng)前路徑才允許跳轉(zhuǎn),否則不跳轉(zhuǎn),同樣推薦提取為統(tǒng)一公共方法
// currentUrl存儲(chǔ)在內(nèi)存中 if (this.$route.path !== currentUrl) { this.$router.push({ path: currentUrl }) }
到此這篇關(guān)于Vue Router路由無法跳轉(zhuǎn)問題匯總的文章就介紹到這了,更多相關(guān)Vue Router路由無法跳轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用vue-video-player插件播放視頻的步驟講解
在最近的項(xiàng)目中有一個(gè)視頻播放的功能,在之前的項(xiàng)目中沒有接觸過類似的功能,第一次接觸,把具體操作步驟一下,這篇文章主要給大家介紹了關(guān)于vue使用vue-video-player插件播放視頻的相關(guān)資料,需要的朋友可以參考下2022-12-12簡(jiǎn)單實(shí)現(xiàn)Vue的observer和watcher
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)Vue的observer和watcher,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12vuex中的state、getters、mutations、actions之間的關(guān)系解讀
這篇文章主要介紹了vuex中的state、getters、mutations、actions之間的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue+three.js實(shí)現(xiàn)炫酷的3D登陸頁面示例詳解
這篇文章主要為大家介紹了vue+three.js實(shí)現(xiàn)炫酷的3D登陸頁面示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07利用vue-i18n實(shí)現(xiàn)多語言切換效果的方法
這篇文章主要給大家介紹了關(guān)于利用vue-i18n實(shí)現(xiàn)多語言切換效果的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue-i18n具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Vue2.5學(xué)習(xí)筆記之如何在項(xiàng)目中使用和配置Vue
這篇文章主要介紹了Vue2.5學(xué)習(xí)筆記之如何在項(xiàng)目中使用和配置Vue的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09