解決vue-router 嵌套路由沒反應的問題
先看下route.js
//route.js
const App = () => import('../App.vue');
const Login = () => import('../component/Login.vue');
const Class = () => import('../component/Class.vue');
const CourseList = () => import('../component/CourseList.vue');
const CourseContent = () => import('../component/CourseContent.vue');
const routers = [{
path:'/',
component:App,
children:[{
path:'login',
component:Login
},{
path:'class',
component:Class
},
{
path:'course',
children:[{
path:'list',
component:CourseList
},{
path:'content',
component:CourseContent
}
]
},
]
}]
export default routers
當你訪問的時候,發(fā)現(xiàn)
http://localhost:8080/#/login
http://localhost:8080/#/class
都正常,但是:
http://localhost:8080/#/course/list
http://localhost:8080/#/course/content
都是一片空白,檢查元素,發(fā)現(xiàn)沒有加載過來。檢查,子路由前面并沒有加/,所以這沒有問題,排除。
其實這是list的父級course沒有component,有了componnet,并且需要在這個component理要有<router-view></router-view>,修改下:
{
path:'course',
component:Course
children:[{
path:'list',
component:CourseList
},{
path:'content',
component:CourseContent
}
]
},
Course.vue如下:
<template> <div> <router-view></router-view> </div> </template>
這樣就可以實現(xiàn)嵌套了。想想,本例子中,其實App組件也是這樣的,他提供了個<router-view></router-view>,對不對?
http://localhost:8080/#/course/list
http://localhost:8080/#/course/content
都可以訪問了。
補充知識:關于Vue-router子路由不顯示的一個坑
遇到這個問題的基本上應該跟我遇到的情況一樣,但是這個問題很隱蔽,老手不會遇到,新人說不清楚,所以爬坑之后來提一下。
父子路由是嵌套關系,所以不能跳過父組件直接顯示子組件。例如我有如下index.js:``
import Vue from 'vue'
import VueRouter from 'vue-router'
//引入兩個組件
import recomView from '../components/views/ty/recom/view.vue'
import recomEdit from '../components/views/ty/recom/edit.vue'
Vue.use(VueRouter)
//建立router
const router = new VueRouter({
mode: 'history',
routes: [
{
path: '/recom',
children: [
{
path: 'view',
name: 'recomView',
component: recomView
},
{
path: 'edit',
name: 'recomEdit',
component: recomEdit
}
]
},
//暴露router,export default方式暴露出的模塊在導入時可以起任何名(不沖突的情況下)
export default router
在某個組件XXX.vue中,我想把recomView和recomEdit兩個組件導入,
<template>
……
<router-view></router-view>
……
</template>
<script>
……
this.$router.push('/recom/view')
this.$router.push('/recom/edit')
……
</script>
然而使用上面的index.js,會發(fā)現(xiàn)無論如何也無法引入。原因在于引入的時候跳級了?;仡^看上面index.js,不妥之處很多,慢慢分析一下
父級路由'/recom'沒有對應的component,導致XXX.vue中的router-view沒有對應的組件,所以此時頁面中你想顯示recomView或者recomEdit的地方會是空白
假如此時把'/recom'處添加component,為方便直接給component: recomView,會發(fā)現(xiàn)你原本指向recomView和recomEdit的兩個路由都會顯示recomView
那么問題就清楚了,其實就是給的路由跳級了:父子路由是一個兩層的嵌套關系,而最上層組件中的router-view只會指向我們這里的父級路由,也就是'/recom',要想指向子路由,那就要再嵌套一層
到這里,解決方法就比較容易想到了,那就是再建立一個組件recom.vue對應父級路由,在recom.vue中再引入一次router-view就行了。
//recom.vue,鑒于我這里并不需要這個'/recom'頁面,所以以最簡單的方式引入router-view就可以了 <template> <router-view></router-view> </template>
那還有沒有更簡單的不需要建立recom.vue方法呢?有。
在index.js中直接引入const recom = {template: `<router-view></router-view>`}就行了。修正后的index.js應該是這樣的:
import Vue from 'vue'
import VueRouter from 'vue-router'
//引入兩個組件
import recomView from '../components/views/ty/recom/view.vue'
import recomEdit from '../components/views/ty/recom/edit.vue'
Vue.use(VueRouter)
//引入recom組件!
const recom = {
template: `<router-view></router-view>`
}
//建立router
const router = new VueRouter({
mode: 'history',
routes: [
{
path: '/recom',
component: recom, //引入recom,必不可少
children: [
{
path: 'view',
name: 'recomView',
component: recomView
},
{
path: 'edit',
name: 'recomEdit',
component: recomEdit
}
]
},
export default router
那還有沒有其他的辦法呢?也有。那就是不要用嵌套路由,直接把子路由升到跟父路由同級就行了,反正你這里又不需要父路由對應的組件,然后給父路由設置一個redirect指向你想默認顯示的頁面,這個相信都會,就不寫了。這也是我一開始想到的辦法,但是上面的bug沒解決心里不舒服,所以耽誤了點時間,不過還好,勉強算是找到原因了。
感覺JS是個很靈活的語言,像我這種半路出家什么書都沒看直接開始做web的,真的要抽時間好好補補基礎了。
以上這篇解決vue-router 嵌套路由沒反應的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue.js實現(xiàn)一個自定義分頁組件vue-paginaiton
這篇文章主要為大家詳細介紹了Vue.js實現(xiàn)一個自定義分頁組件vue-paginaiton的具體代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Vue使用antd組件a-form-model實現(xiàn)數(shù)據(jù)連續(xù)添加功能
這篇文章主要介紹了Vue使用antd組件a-form-model實現(xiàn)數(shù)據(jù)連續(xù)添加功能,本文結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
vue音樂播放器插件vue-aplayer的配置及其使用實例詳解
本篇文章主要介紹了vue音樂播放器插件vue-aplayer的配置及其使用實例詳解,具有一定的參考價值,有興趣的可以了解一下2017-07-07

