Vue實(shí)現(xiàn)路由嵌套的方法實(shí)例
1、嵌套路由又稱子路由,在實(shí)際應(yīng)用中,通常由多層嵌套的組件組合而成。(其實(shí)就是套娃操作罷了,跟后端的視圖跳轉(zhuǎn)路徑蠻像的):
2、 創(chuàng)建用戶信息組件,在 views/user 目錄下創(chuàng)建一個名為 Profile.vue 的視圖組件:
Profile.vue
<template> <h1>咸魚_翻身1</h1> </template> <script> export default { name: "UserList" } </script> <style scoped> </style>
3、在用戶列表組件在 views/user 目錄下創(chuàng)建一個名為 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">個人信息</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>個人信息</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、那么我們加一個功能呢:
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue $mount實(shí)戰(zhàn)之實(shí)現(xiàn)消息彈窗組件
這篇文章主要介紹了Vue $mount實(shí)現(xiàn)消息彈窗組件的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04vue中ts無法識別引入的vue文件,提示找不到xxx.vue模塊的解決
這篇文章主要介紹了vue中ts無法識別引入的vue文件,提示找不到xxx.vue模塊的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Vue全局監(jiān)測錯誤并生成錯誤日志實(shí)現(xiàn)方法介紹
在做完一個項(xiàng)目后,之后的維護(hù)尤為重要。這時,如果項(xiàng)目配置了錯誤日志記錄,這樣能大大減少維護(hù)難度。雖然不一定能捕獲到全部的錯誤,但是一般的錯誤還是可以監(jiān)測到的。這樣就不用測試人員去一遍一遍復(fù)現(xiàn)bug了2022-10-10Vue實(shí)現(xiàn)Google第三方登錄的示例代碼
本文記錄作者在vue項(xiàng)目中使用到Google第三方登錄,查詢到的資料文檔也不詳細(xì),故此把自己所遇到的坑及問題詳細(xì)的記錄下來。2021-07-07vue計算屬性時v-for處理數(shù)組時遇到的一個bug問題
這篇文章主要介紹了在做vue計算屬性,v-for處理數(shù)組時遇到的一個bug 問題,需要的朋友可以參考下2018-01-01vue-cli-service和webpack-dev-server的區(qū)別及說明
這篇文章主要介紹了vue-cli-service和webpack-dev-server的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10