Vue實(shí)現(xiàn)路由嵌套的方法實(shí)例
1、嵌套路由又稱子路由,在實(shí)際應(yīng)用中,通常由多層嵌套的組件組合而成。(其實(shí)就是套娃操作罷了,跟后端的視圖跳轉(zhuǎn)路徑蠻像的):

2、 創(chuàng)建用戶信息組件,在 views/user 目錄下創(chuàng)建一個(gè)名為 Profile.vue 的視圖組件:
Profile.vue
<template>
<h1>咸魚_翻身1</h1>
</template>
<script>
export default {
name: "UserList"
}
</script>
<style scoped>
</style>
3、在用戶列表組件在 views/user 目錄下創(chuàng)建一個(gè)名為 List.vue 的視圖組件:
List.vue
<template>
<h1>咸魚_翻身2</h1>
</template>
<script>
export default {
name: "UserList"
}
</script>
<style scoped>
</style>
4、修改首頁視圖,我們修改 Main.vue 視圖組件,此處使用了 ElementUI 布局容器組件,代碼如下:
Main.vue
<template>
<div>
<el-container>
<el-aside width="200px">
<el-menu :default-openeds="['1']">
<el-submenu index="1">
<template slot="title"><i class="el-icon-caret-right"></i>用戶管理</template>
<el-menu-item-group>
<el-menu-item index="1-1">
<!--插入的地方-->
<router-link to="/user/profile">個(gè)人信息</router-link>
</el-menu-item>
<el-menu-item index="1-2">
<!--插入的地方-->
<router-link to="/user/list">用戶列表</router-link>
</el-menu-item>
</el-menu-item-group>
</el-submenu>
<el-submenu index="2">
<template slot="title"><i class="el-icon-caret-right"></i>內(nèi)容管理</template>
<el-menu-item-group>
<el-menu-item index="2-1">分類管理</el-menu-item>
<el-menu-item index="2-2">內(nèi)容列表</el-menu-item>
</el-menu-item-group>
</el-submenu>
</el-menu>
</el-aside>
<el-container>
<el-header style="text-align: right; font-size: 12px">
<el-dropdown>
<i class="el-icon-setting" style="margin-right: 15px"></i>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>個(gè)人信息</el-dropdown-item>
<el-dropdown-item>退出登錄</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</el-header>
<el-main>
<!--在這里展示視圖-->
<router-view />
</el-main>
</el-container>
</el-container>
</div>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped lang="scss">
.el-header {
background-color: #B3C0D1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
5、配置嵌套路由修改 router 目錄下的 index.js 路由配置文件,使用children放入main中寫入子模塊,代碼如下:
index.js
//導(dǎo)入vue
import Vue from 'vue';
import VueRouter from 'vue-router';
//導(dǎo)入組件
import Main from "../views/Main";
import Login from "../views/Login";
//導(dǎo)入子模塊
import UserList from "../views/user/List";
import UserProfile from "../views/user/Profile";
//使用
Vue.use(VueRouter);
//導(dǎo)出
export default new VueRouter({
routes: [
{
//登錄頁
path: '/main',
component: Main,
// 寫入子模塊
children: [
{
path: '/user/profile',
component: UserProfile,
}, {
path: '/user/list',
component: UserList,
},
]
},
//首頁
{
path: '/login',
component: Login
},
]
})
6、運(yùn)行結(jié)果:


7、項(xiàng)目結(jié)構(gòu)為:

8、那么我們加一個(gè)功能呢:
Main.vue中加入這段代碼即可:
<el-submenu index="3">
<template slot="title"><i class="el-icon-caret-right"></i>咸魚_翻身管理</template>
<el-menu-item-group>
<el-menu-item index="3-1">咸魚_翻身4</el-menu-item>
<el-menu-item index="3-2">咸魚_翻身5</el-menu-item>
</el-menu-item-group>
</el-submenu>

總結(jié)
到此這篇關(guān)于Vue實(shí)現(xiàn)路由嵌套的文章就介紹到這了,更多相關(guān)Vue路由嵌套內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue組件中點(diǎn)擊按鈕后修改輸入框的狀態(tài)實(shí)例代碼
要求點(diǎn)擊修改按鈕之后部分輸入框由禁用狀態(tài)變?yōu)榭捎脿顟B(tài)。下面我給大家分享一段實(shí)例代碼基于vue組件中點(diǎn)擊按鈕后修改輸入框的狀態(tài),需要的的朋友參考下2017-04-04
vue使用async/await來實(shí)現(xiàn)同步和異步的案例分享
近期項(xiàng)目中大量使用async,從服務(wù)器獲取數(shù)據(jù),解決一些并發(fā)傳參問題,代碼很簡單,在此也看了一些博客,現(xiàn)在async/await已經(jīng)大范圍讓使用,所以本文給大家介紹一下vue使用async/await來實(shí)現(xiàn)同步和異步的案例,需要的朋友可以參考下2024-01-01
VUE 單頁面使用 echart 窗口變化時(shí)的用法
這篇文章主要介紹了VUE 單頁面使用 echart 窗口變化時(shí)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較
這篇文章主要為大家介紹了vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Vue實(shí)現(xiàn)登錄記住賬號(hào)密碼功能的思路與過程
最近在學(xué)習(xí)vue,發(fā)現(xiàn)了vue的好多坑,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)登錄記住賬號(hào)密碼功能的思路與過程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11

