Vue2之嵌套路由詳解
數(shù)據(jù)準(zhǔn)備
- src/router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
mode: 'history',
routes: [
// 配置重定向
{ path: '/', redirect: '/find' },
{ path: '/find', component: ()=> import('@/views/Find.vue')},
{ path: '/my', component: () => import('@/views/My.vue')},
{ path: '/friend', component: () => import('@/views/Friend.vue')}
]
})
export default router
- src/app.vue
<template>
<div>
<div class="footer_wrap">
<router-link to="/find?id=1">發(fā)現(xiàn)音樂</router-link>
<router-link to="/my">我的音樂</router-link>
<router-link to="/friend">朋友</router-link>
</div>
<div class="top">
<!-- 路由出口 → 匹配的組件所展示的位置 -->
<router-view></router-view>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style lang="scss">
body {
margin: 0;
padding: 0;
}
.footer_wrap {
position: relative;
left: 0;
top: 0;
display: flex;
width: 100%;
text-align: center;
background-color: #333;
color: #ccc;
}
.footer_wrap a {
flex: 1;
text-decoration: none;
padding: 20px 0;
line-height: 20px;
background-color: #333;
color: #ccc;
border: 1px solid black;
}
// 路由激活樣式
.footer_wrap .router-link-active{
background-color: purple;
}
</style>
- src/views/Find.vue
<template>
<div>
<p>發(fā)現(xiàn)音樂</p>
<p>發(fā)現(xiàn)音樂</p>
<p>發(fā)現(xiàn)音樂</p>
<p>發(fā)現(xiàn)音樂</p>
</div>
</template>
<script>
export default {
name: 'FindMusic'
}
</script>
<style>
</style>
- src/views/Friend.vue
<template>
<div>
<p>我的朋友</p>
<p>我的朋友</p>
<p>我的朋友</p>
<p>我的朋友</p>
</div>
</template>
<script>
export default {
name: 'MyFriend'
}
</script>
<style>
</style>
- src/views/my.vue
<template>
<div>
<p>我的音樂</p>
<p>我的音樂</p>
<p>我的音樂</p>
<p>我的音樂</p>
</div>
</template>
<script>
export default {
name: 'MyMusic'
}
</script>
<style>
</style>
項(xiàng)目目錄

運(yùn)行結(jié)果

什么是嵌套路由
通過路由實(shí)現(xiàn)組件的嵌套展示,叫做嵌套路由。
實(shí)際生活中的應(yīng)用界面,通常由多層嵌套的組件組合而成。同樣地,URL 中各段動(dòng)態(tài)路徑也按某種結(jié)構(gòu)對(duì)應(yīng)嵌套的各層組件
語法
語法1(推薦)
// ...
const router = new VueRouter({
// ...
routes: [
// 配置重定向
{ path: '/', redirect: '/find' },
{ path: '/find', component: ()=> import('@/views/Find.vue'),
// 配置子路由的重定向
redirect: '/find/son1',
// 配置子路由,子路由前面沒有/。這種寫法沒有冗余
children:[
{ path: 'son1', component: () => import('@/views/find/Son1.vue')},
{ path: 'son2', component: () => import('@/views/find/Son2.vue')},
]
},
{ path: '/my', component: () => import('@/views/My.vue')},
{ path: '/friend', component: () => import('@/views/Friend.vue')}
]
})
// ...
語法2
// ...
const router = new VueRouter({
// ...
routes: [
// 配置重定向
{ path: '/', redirect: '/find' },
{ path: '/find', component: ()=> import('@/views/Find.vue'),
// 配置子路由
children:[
// 配置子路由的重定向 這種寫法前面有冗余
{ path: '/find',redirect: '/find/son1'},
{ path: '/find/son1', component: () => import('@/views/find/Son1.vue')},
{ path: '/find/son2', component: () => import('@/views/find/Son2.vue')},
]
},
{ path: '/my', component: () => import('@/views/My.vue')},
{ path: '/friend', component: () => import('@/views/Friend.vue')}
]
})
// ...
代碼演示
- 新建src/views/find/Son1.vue
<template>
<div style="border: 1px solid red; padding: 20px">
<h3>我是find的子路由son1</h3>
</div>
</template>
<script>
export default {
}
</script>
<style>
</style>
- 新建src/views/find/Son2.vue
<template>
<div style="border: 1px solid red; padding: 20px">
<h3>我是find的子路由son2</h3>
</div>
</template>
<script>
export default {
}
</script>
<style>
</style>
- 修改src/router/index.js(第一種語法)
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
mode: 'history',
routes: [
// 配置重定向
{ path: '/', redirect: '/find' },
{ path: '/find', component: ()=> import('@/views/Find.vue'),
// 配置子路由的重定向
redirect: '/find/son1',
// 配置子路由
children:[
{ path: 'son1', component: () => import('@/views/find/Son1.vue')},
{ path: 'son2', component: () => import('@/views/find/Son2.vue')},
]
},
{ path: '/my', component: () => import('@/views/My.vue')},
{ path: '/friend', component: () => import('@/views/Friend.vue')}
]
})
export default router
- 修改Find.vue
<template>
<div>
<!-- 添加二級(jí)路由 -->
<div class="childernNav">
<router-link to="/find/son1">子組件1</router-link>
<router-link to="/find/son2">子組件2</router-link>
</div>
<!-- 添加二級(jí)路由的視圖 -->
<router-view></router-view>
<p>發(fā)現(xiàn)音樂</p>
<p>發(fā)現(xiàn)音樂</p>
<p>發(fā)現(xiàn)音樂</p>
<p>發(fā)現(xiàn)音樂</p>
</div>
</template>
<script>
export default {
name: "FindMusic",
};
</script>
<style lang="scss">
.childernNav {
display: flex;
margin: 20px 0 ;
a {
// 清除a標(biāo)簽的默認(rèn)樣式
text-decoration: none;
color: #000;
display: block;
width: 100px;
height: 50px;
line-height: 50px;
text-align: center;
background-color: #ccc;
}
.router-link-active {
background-color: green;
}
}
</style>
運(yùn)行流程

運(yùn)行結(jié)果

結(jié)合ElementUI實(shí)戰(zhàn)
請(qǐng)參考我之前的博客ElementUI之菜單(Menu)使用
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解VUE Element-UI多級(jí)菜單動(dòng)態(tài)渲染的組件
這篇文章主要介紹了VUE Element-UI多級(jí)菜單動(dòng)態(tài)渲染的組件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue數(shù)據(jù)與事件綁定以及Class和Style的綁定詳細(xì)講解
這篇文章主要介紹了Vue數(shù)據(jù)與事件綁定以及Class和Style的綁定,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
vue實(shí)現(xiàn)多個(gè)el-form表單提交統(tǒng)一校驗(yàn)的2個(gè)方法
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)多個(gè)el-form表單提交統(tǒng)一校驗(yàn)的2個(gè)方法,文中通過代碼示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或使用vue具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
解決Vue的文本編輯器 vue-quill-editor 小圖標(biāo)樣式排布錯(cuò)亂問題
這篇文章主要介紹了解決Vue的文本編輯器 vue-quill-editor 小圖標(biāo)樣式排布錯(cuò)亂問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue3+TypeScript項(xiàng)目架構(gòu)實(shí)踐指南
在現(xiàn)代前端開發(fā)中,Vue3和Vite的組合已經(jīng)成為許多開發(fā)者的首選,這篇文章主要介紹了Vue3+TypeScript項(xiàng)目架構(gòu)實(shí)踐的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-02-02

