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 $mount實(shí)戰(zhàn)之實(shí)現(xiàn)消息彈窗組件
這篇文章主要介紹了Vue $mount實(shí)現(xiàn)消息彈窗組件的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue中ts無法識(shí)別引入的vue文件,提示找不到xxx.vue模塊的解決
這篇文章主要介紹了vue中ts無法識(shí)別引入的vue文件,提示找不到xxx.vue模塊的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue全局監(jiān)測(cè)錯(cuò)誤并生成錯(cuò)誤日志實(shí)現(xiàn)方法介紹
在做完一個(gè)項(xiàng)目后,之后的維護(hù)尤為重要。這時(shí),如果項(xiàng)目配置了錯(cuò)誤日志記錄,這樣能大大減少維護(hù)難度。雖然不一定能捕獲到全部的錯(cuò)誤,但是一般的錯(cuò)誤還是可以監(jiān)測(cè)到的。這樣就不用測(cè)試人員去一遍一遍復(fù)現(xiàn)bug了2022-10-10
Vue實(shí)現(xiàn)Google第三方登錄的示例代碼
本文記錄作者在vue項(xiàng)目中使用到Google第三方登錄,查詢到的資料文檔也不詳細(xì),故此把自己所遇到的坑及問題詳細(xì)的記錄下來。2021-07-07
vue計(jì)算屬性時(shí)v-for處理數(shù)組時(shí)遇到的一個(gè)bug問題
這篇文章主要介紹了在做vue計(jì)算屬性,v-for處理數(shù)組時(shí)遇到的一個(gè)bug 問題,需要的朋友可以參考下2018-01-01
Vue中路由參數(shù)與查詢參數(shù)傳遞對(duì)比解析
在Vue.js中,路由與導(dǎo)航不僅涉及頁面切換,還包括了向頁面?zhèn)鬟f參數(shù)和獲取查詢參數(shù),這篇文章主要介紹了Vue路由參數(shù)與查詢參數(shù)傳遞,需要的朋友可以參考下2023-08-08
Vue項(xiàng)目中使用flow做類型檢測(cè)的方法
這篇文章主要介紹了Vue項(xiàng)目中使用flow做類型檢測(cè)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
vue-cli-service和webpack-dev-server的區(qū)別及說明
這篇文章主要介紹了vue-cli-service和webpack-dev-server的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

