Vue 動(dòng)態(tài)添加路由及生成菜單的方法示例
寫后臺(tái)管理系統(tǒng),估計(jì)有不少人遇過這樣的需求:根據(jù)后臺(tái)數(shù)據(jù)動(dòng)態(tài)添加路由和菜單。
為什么這么做呢?因?yàn)椴煌挠脩粲胁煌臋?quán)限,能訪問的頁面是不一樣的。
在網(wǎng)上找了好多資料,終于想到了解決辦法。
動(dòng)態(tài)生成路由
利用 vue-router 的 addRoutes 方法可以動(dòng)態(tài)添加路由。
先看一下官方介紹:
router.addRoutes
router.addRoutes(routes: Array<RouteConfig>)
動(dòng)態(tài)添加更多的路由規(guī)則。參數(shù)必須是一個(gè)符合 routes 選項(xiàng)要求的數(shù)組。
舉個(gè)例子:
const router = new Router({
routes: [
{
path: '/login',
name: 'login',
component: () => import('../components/Login.vue')
},
{path: '/', redirect: '/home'},
]
})
上面的代碼和下面的代碼效果是一樣的
const router = new Router({
routes: [
{path: '/', redirect: '/home'},
]
})
router.addRoutes([
{
path: '/login',
name: 'login',
component: () => import('../components/Login.vue')
}
])
在動(dòng)態(tài)添加路由的過程中,如果有 404 頁面,一定要放在最后添加,否則在登陸的時(shí)候添加完頁面會(huì)重定向到 404 頁面。
類似于這樣,這種規(guī)則一定要最后添加。
{path: '*', redirect: '/404'}
動(dòng)態(tài)生成菜單
假設(shè)后臺(tái)返回來的數(shù)據(jù)長這樣
// 左側(cè)菜單欄數(shù)據(jù)
menuItems: [
{
name: 'home', // 要跳轉(zhuǎn)的路由名稱 不是路徑
size: 18, // icon大小
type: 'md-home', // icon類型
text: '主頁' // 文本內(nèi)容
},
{
text: '二級(jí)菜單',
type: 'ios-paper',
children: [
{
type: 'ios-grid',
name: 't1',
text: '表格'
},
{
text: '三級(jí)菜單',
type: 'ios-paper',
children: [
{
type: 'ios-notifications-outline',
name: 'msg',
text: '查看消息'
},
{
type: 'md-lock',
name: 'password',
text: '修改密碼'
},
{
type: 'md-person',
name: 'userinfo',
text: '基本資料',
}
]
}
]
}
]
來看看怎么將它轉(zhuǎn)化為菜單欄,我在這里使用了 iview 的組件,不用重復(fù)造輪子。
<!-- 菜單欄 -->
<Menu ref="asideMenu" theme="dark" width="100%" @on-select="gotoPage"
accordion :open-names="openMenus" :active-name="currentPage" @on-open-change="menuChange">
<!-- 動(dòng)態(tài)菜單 -->
<div v-for="(item, index) in menuItems" :key="index">
<Submenu v-if="item.children" :name="index">
<template slot="title">
<Icon :size="item.size" :type="item.type"/>
<span v-show="isShowAsideTitle">{{item.text}}</span>
</template>
<div v-for="(subItem, i) in item.children" :key="index + i">
<Submenu v-if="subItem.children" :name="index + '-' + i">
<template slot="title">
<Icon :size="subItem.size" :type="subItem.type"/>
<span v-show="isShowAsideTitle">{{subItem.text}}</span>
</template>
<MenuItem class="menu-level-3" v-for="(threeItem, k) in subItem.children" :name="threeItem.name" :key="index + i + k">
<Icon :size="threeItem.size" :type="threeItem.type"/>
<span v-show="isShowAsideTitle">{{threeItem.text}}</span>
</MenuItem>
</Submenu>
<MenuItem v-else v-show="isShowAsideTitle" :name="subItem.name">
<Icon :size="subItem.size" :type="subItem.type"/>
<span v-show="isShowAsideTitle">{{subItem.text}}</span>
</MenuItem>
</div>
</Submenu>
<MenuItem v-else :name="item.name">
<Icon :size="item.size" :type="item.type" />
<span v-show="isShowAsideTitle">{{item.text}}</span>
</MenuItem>
</div>
</Menu>
代碼不用看得太仔細(xì),理解原理即可,其實(shí)就是通過三次 v-for 不停的對(duì)子數(shù)組進(jìn)行循環(huán),生成三級(jí)菜單。
動(dòng)態(tài)菜單這樣就可以實(shí)現(xiàn)了。
動(dòng)態(tài)路由,因?yàn)樯厦嬉呀?jīng)說過了用 addRoutes 來實(shí)現(xiàn),現(xiàn)在看看具體怎么做。
首先,要把項(xiàng)目所有的頁面路由都列出來,再用后臺(tái)返回來的數(shù)據(jù)動(dòng)態(tài)匹配,能匹配上的就把路由加上,不能匹配上的就不加。
最后把這個(gè)新生成的路由數(shù)據(jù)用 addRoutes 添加到路由表里。
const asyncRoutes = {
'home': {
path: 'home',
name: 'home',
component: () => import('../views/Home.vue')
},
't1': {
path: 't1',
name: 't1',
component: () => import('../views/T1.vue')
},
'password': {
path: 'password',
name: 'password',
component: () => import('../views/Password.vue')
},
'msg': {
path: 'msg',
name: 'msg',
component: () => import('../views/Msg.vue')
},
'userinfo': {
path: 'userinfo',
name: 'userinfo',
component: () => import('../views/UserInfo.vue')
}
}
// 傳入后臺(tái)數(shù)據(jù) 生成路由表
menusToRoutes(menusData)
// 將菜單信息轉(zhuǎn)成對(duì)應(yīng)的路由信息 動(dòng)態(tài)添加
function menusToRoutes(data) {
const result = []
const children = []
result.push({
path: '/',
component: () => import('../components/Index.vue'),
children,
})
data.forEach(item => {
generateRoutes(children, item)
})
children.push({
path: 'error',
name: 'error',
component: () => import('../components/Error.vue')
})
// 最后添加404頁面 否則會(huì)在登陸成功后跳到404頁面
result.push(
{path: '*', redirect: '/error'},
)
return result
}
function generateRoutes(children, item) {
if (item.name) {
children.push(asyncRoutes[item.name])
} else if (item.children) {
item.children.forEach(e => {
generateRoutes(children, e)
})
}
}
所有的代碼實(shí)現(xiàn),我都放在 github 上,動(dòng)態(tài)菜單的實(shí)現(xiàn)放在這個(gè)項(xiàng)目下的 src/components/Index.vue、src/permission.js 和 src/utils/index.js下
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue 使用class創(chuàng)建和清除水印的示例代碼
這篇文章主要介紹了vue 使用class創(chuàng)建和清除水印的示例代碼,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
Vue中的驗(yàn)證登錄狀態(tài)的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue中的驗(yàn)證登錄狀態(tài)的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
Vue Element前端應(yīng)用開發(fā)之界面語言國際化
我們開發(fā)的系統(tǒng),一般可以不用考慮語言國際化的問題,大多數(shù)系統(tǒng)一般是給本國人使用的,而且直接使用中文開發(fā)界面會(huì)更加迅速 一些,不過框架最好能夠支持國際化的處理,以便在需要的時(shí)候,可以花點(diǎn)時(shí)間來實(shí)現(xiàn)多語言切換的處理,使系統(tǒng)具有更廣泛的受眾用戶。2021-05-05
vue3中reactive和ref函數(shù)及對(duì)比分析
這篇文章主要介紹了vue3中reactive和ref函數(shù)及對(duì)比,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01
vue項(xiàng)目通過a標(biāo)簽下載圖片至zip包的示例代碼
在vue項(xiàng)目中,將圖片下載可使用流的形式,下載成單個(gè)圖片,或者將多個(gè)圖片下載至zip包,本文就是介紹使用a標(biāo)簽下載圖片的用法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10
解決vue-router進(jìn)行build無法正常顯示路由頁面的問題
下面小編就為大家分享一篇解決vue-router進(jìn)行build無法正常顯示路由頁面的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Vue+jquery實(shí)現(xiàn)表格指定列的文字收縮的示例代碼
這篇文章主要介紹了Vue+jquery實(shí)現(xiàn)表格指定列的文字收縮的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
vue中watch和computed的區(qū)別與使用方法
這篇文章主要給大家介紹了關(guān)于vue中watch和computed的區(qū)別與使用方法的相關(guān)資料,文中通過實(shí)例代碼結(jié)束的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

