Ant Design Vue如何生成動態(tài)菜單a-menu
今天,小編帶你們看一看從官網(wǎng)總結(jié)來得動態(tài)菜單
一、定義template模板
<template>
<a-layout>
<!-- 左側(cè)導(dǎo)航 -->
<a-layout-sider>
<div>
<a-menu
:inlineIndent="inlineIndent" 菜單縮進(jìn)
:defaultSelectedKeys="[$route.path]" 默認(rèn)選中的節(jié)點
:openKeys="openKeys" 展開的節(jié)點
mode="inline" 菜單模式
:inline-collapsed="collapsed" 折疊方式
@openChange="onOpenChange"
@click="menuClick">
<!-- 菜單遍歷的開始 -->
<template v-for="item in menuList">
<!-- 如果當(dāng)前遍歷項沒有children,視為子菜單項,注意所有的key都是path用于路由跳轉(zhuǎn),以及當(dāng)前選中記錄 -->
<a-menu-item v-if="!item.children" :key="item.menu_url">
<i :class="item.menu_icon" style="font-size:18px;margin-right:5px"/>
<span style="font-size: 15px;">{{ item.menu_name }}</span>
</a-menu-item>
<!-- 否則視為子菜單,傳入菜單信息并且運用下面定義的函數(shù)式組件 -->
<sub-menu v-else :key="item.menu_url" :menu-info="item" />
</template>
</a-menu>
</div>
</a-layout-sider>
<!-- 內(nèi)容 -->
<a-layout-content>
<router-view></router-view>
</a-layout-content>
</a-layout>
</template>
二、定義函數(shù)式組件
// 定義函數(shù)式組件
const SubMenu = {
template: `
<a-sub-menu :key="menuInfo.menu_url" v-bind="$props" v-on="$listeners">
<span slot="title">
<i class="iconfont iconshezhiziduan" v-if="menuInfo.menu_name=='系統(tǒng)管理'" style="font-size:18px;margin-right:5px"/>
<span style="font-size: 15px;">{{ menuInfo.menu_name }}</span>
</span>
<template v-for="item in menuInfo.children">
<a-menu-item v-if="!item.children" :key="item.menu_url">
<i :class="item.menu_icon" style="font-size:18px;margin-right:5px"/>
<span style="font-size: 15px;">{{ item.menu_name }}</span>
</a-menu-item>
<sub-menu v-else :key="item.menu_url" :menu-info="item" />
</template>
</a-sub-menu>
`,
三、引入菜單組件及接受動態(tài)菜單數(shù)據(jù)
import { Menu } from 'ant-design-vue';
name: 'SubMenu',
// true 此項必須被定義
isSubMenu: true,
props: {
// 解構(gòu)a-sub-menu的屬性,也就是文章開頭提到的為什么使用函數(shù)式組件
...Menu.SubMenu.props,
// 接收父級傳遞過來的菜單信息
menuInfo: {
type: Object,
default: () => ({}),
},
},
動態(tài)菜單數(shù)據(jù)格式如下:
// 菜單數(shù)據(jù)
menuList: [
{
key:'1',
title: '系統(tǒng)信息管理',
path: '/system_infomation_manage',
icon:'iconfont iconshezhiziduan',
children: [
{
key:'2',
title: '項目信息管理',
path: '/system_base/system_information',
icon:''
},
{
key:'3',
title: '系統(tǒng)組織機構(gòu)管理',
path: '/system_base/institul_framework',
icon:''
},
{
key:'4',
title: '系統(tǒng)人員管理',
path: '/system_base/personnel_manage',
icon:''
},
{
key:'5',
title: '系統(tǒng)權(quán)限管理',
path: '/system_base/jurisdiction_manage',
icon:''
},
{
key:'6',
title:'項目業(yè)務(wù)字典管理',
path:'/system_dictionary_management',
icon:'',
children:[
{
key:'6_1',
title:'材料設(shè)備管理',
path:'/dictionary_material_manage',
icon:'',
children:[
{
key:'6_1_1',
title:'材料管理',
path:'/system_base/material_manage',
icon:'',
},
{
key:'6_1_2',
title:'機械設(shè)備管理',
path:'/system_base/machine_manage',
icon:'',
}
]
}
]
}
]
}
],
四、定義其他數(shù)據(jù)
// 菜單縮進(jìn) inlineIndent:12, // 默認(rèn)不折疊 collapsed: false, // 全部父節(jié)點 rootSubmenuKeys: ['/system_infomation_manage'], openKeys: [],//默認(rèn)展開的節(jié)點 defaultOpenKeys:['/system_infomation_manage'], // 選中的子菜單項 defaultSelectedKeys: [this.$route.path],
五、所涉及到的方法
methods:{
// 控制只打開一個
onOpenChange(openKeys) {
// 將當(dāng)前打開的父級菜單存入緩存中
window.localStorage.setItem('systemOpenKeys', JSON.stringify(openKeys))
const latestOpenKey = openKeys.find(key => this.openKeys.indexOf(key) === -1);
if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
this.openKeys = openKeys;
} else {
this.openKeys = latestOpenKey ? [latestOpenKey] : [];
}
},
// 點擊菜單,路由跳轉(zhuǎn),注意的是當(dāng)點擊MenuItem才會觸發(fā)此函數(shù)
menuClick({ item, key, keyPath }) {
// 獲取到當(dāng)前的key,并且跳轉(zhuǎn)
this.$router.push({
path: key
})
},
},
created(){
// 將從緩存中取出openKeys
const openKeys = window.localStorage.getItem('systemOpenKeys')
if(openKeys){
// 存在即賦值
this.openKeys = JSON.parse(openKeys)
}else{
this.openKeys = ['/system_infomation_manage']
}
this.getSystemPermission()
},
這樣,一個完整的動態(tài)菜單就渲染出來了,最重要的一步就是定義函數(shù)式組件,這也是Vue和React框架的重要思想之一。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue 路由跳轉(zhuǎn)四種方式實踐案例 (帶參數(shù))
本文詳細(xì)介紹了Vue中通過$router對象實現(xiàn)的四種路由跳轉(zhuǎn)方法:router-link的使用、this.$router.push()和this.$router.replace(),以及參數(shù)傳遞的query與params區(qū)別,感興趣的朋友跟隨小編一起看看吧2025-05-05
Vue使用Element折疊面板Collapse如何設(shè)置默認(rèn)全部展開
這篇文章主要介紹了Vue使用Element折疊面板Collapse如何設(shè)置默認(rèn)全部展開,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
vue中v-model如何綁定多循環(huán)表達(dá)式實戰(zhàn)案例
v-model綁定的變量無論是對象還是數(shù)組都是綁定的value值,下面這篇文章主要給大家介紹了關(guān)于vue中v-model如何綁定多循環(huán)表達(dá)式的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
使用Vue.js開發(fā)微信小程序開源框架mpvue解析
這篇文章主要介紹了使用Vue.js開發(fā)微信小程序開源框架mpvue解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
vuex中store.commit和store.dispatch的區(qū)別及使用方法
這篇文章主要介紹了vuex中store.commit和store.dispatch的區(qū)別及使用方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

