vue 自定義側(cè)邊欄 遞歸無限子級(jí)菜單

有很多網(wǎng)站會(huì)涉及到導(dǎo)航欄,我自己在開發(fā)中用的是element導(dǎo)航組件,并且自定義事件,給大家分享一下。

1.使用遞歸方法,無限循環(huán)子級(jí)菜單。

2.使用組件封裝,維護(hù)方便。

3.使用index作為路由跳轉(zhuǎn)。

4.結(jié)合后臺(tái)動(dòng)態(tài)渲染。

5.自定義樣式,修改默認(rèn)icon。

6.根據(jù)后臺(tái)返回?cái)?shù)據(jù),動(dòng)態(tài)設(shè)置默認(rèn)選中。



?1.封裝組件。

  首先創(chuàng)建好vue之后,在a_module里創(chuàng)建一個(gè)存放組件的文件夾,因?yàn)閍_module是存放公共資源以及靜態(tài)文件的,當(dāng)然也可以根據(jù)喜好來創(chuàng)建。




2.調(diào)用組件。

  在要使用到的頁面中使用 import 引入封裝好的組件,components初始化一下,在頁面中使用。






注意:這里需要注意的是,在引用的頁面中一定要el-menu包裹起來,可以在el-menu標(biāo)簽里來配置側(cè)邊欄的各種狀態(tài)。

3.組件傳值



?我是后臺(tái)拿到的數(shù)據(jù),當(dāng)然也可以自定義數(shù)據(jù),在引用組建的頁面內(nèi)創(chuàng)建,在子組件標(biāo)簽上邊自定義事件,在子組件用props接收參數(shù)。



?4.el-menu配置

  這里呢,需要在父組件內(nèi)配置側(cè)邊欄的各種狀態(tài),選中狀態(tài)啦,index路由跳轉(zhuǎn)啦,等等。



?  這里呢,我主要配置了,router:開啟index跳轉(zhuǎn)路由,default-active:默認(rèn)選中,這里呢我做了很多判斷,大家可以根據(jù)實(shí)際情況來做處理,我主要是拿到當(dāng)前地址,來做選中狀態(tài)。active-text-color:配置字體顏色,等等

5.子組件遞歸,無限循環(huán)子級(jí)

我呢用的是遞歸的方法,他的原理呢就是自己調(diào)用自己。



?注意看紅色框中的內(nèi)容,

(1).大家可以看到,我在跟標(biāo)簽里邊寫了一個(gè):class,主要原因就是,但你循環(huán)自己的時(shí)候,設(shè)置css樣式只會(huì)生效一次,這個(gè)可以解決這個(gè)問題。

(2).然后呢,根據(jù)數(shù)據(jù)結(jié)構(gòu),循環(huán)父組件傳遞過來的參數(shù),如果沒有children的話,循環(huán)下邊的代碼,不調(diào)用子級(jí),如果有children的話證明有子級(jí)菜單,那么調(diào)用上邊代碼。

(3).大家可以看到在label標(biāo)簽里我又調(diào)用了自己,然后傳遞參數(shù),這里就是遞歸,如果滿足條件就無線調(diào)用自己。

(4).這里呢我用了自定義圖標(biāo),大家可以觀察我的布局。

(5).到這里已經(jīng)差不多了,但是要注意的是,:index,接受的是字符串形式的值,所以這里比較坑,需要的話可以用String()轉(zhuǎn)化一下。

(6).至于圖片里getImgUrl()方法,是解決圖片異步的一種方法。



?6.默認(rèn)樣式

很多框架的默認(rèn)樣式是很難修改的,大家可以在頁面中去找的class類名去修改,我用的是 /deep/ 這個(gè)是一個(gè)深度修飾符 或者是 >>> ,當(dāng)然也可以在跟文件里去修改樣式,但是我不建議這樣去做,因?yàn)闀?huì)他是全局修改,容易影響其他頁面的樣式。


差不多就是這些了,樣式的以及源碼我在下邊給大家附上。

父組件:

html:

<el-menu

:router="true"

:default-active="(this.$route.meta[0].meta&&this.$route.meta[0].meta[0].path&&!this.$route.meta[0].meta[0].ispath)?this.$route.meta[0].meta[0].path:this.$route.meta[0].path"

active-text-color="#ffffff"

:unique-opened="true"

background-color="#00bfb8"

>

<menutree :data="navigation" class="hr_one"></menutree>

<!-- 側(cè)邊導(dǎo)航組件 -->

</el-menu>


css:

.el-aside::-webkit-scrollbar {

width: 4px;

background: #00d2ca;

}

.el-aside::-webkit-scrollbar-thumb {

background: #009d97;

border-top-right-radius: 10px;

border-top-left-radius: 10px;

border-bottom-right-radius: 10px;

border-bottom-left-radius: 10px;

}

.el-main::-webkit-scrollbar {

display: none;

}

.el-aside {

text-align: center;

position: absolute;

top: 80px;

bottom: 0;

left: 0;

background-color: #00bfb8 !important;

min-height: 800px;

.el-menu-item-group /deep/.el-menu-item-group__title {

padding: 0 !important;

}

.el-menu-item:hover {

background-color: #00d2ca;

}

.child_li {

color: #ffff;

display: flex;

align-items: center;

padding: 0;

span:first-child {

margin-left: 23px;

}

span:last-child {

margin-left: 23px;

}

}

}

josn:

這個(gè)數(shù)據(jù)呢,是我自己模擬的數(shù)據(jù)

navigation: [

{

?id:0,

?name: "首頁",

?path: "/Hr-index/home",

icon: "icon_hrIndex.png"

?},

?{

?id:1,

?name: "教職工檔案",

path: "/Hr-index/staffFiles",

icon: "icon_staffFiles.png"

},

?{

?id:2,

?name: "招聘信息",

?path: "",

?icon: "icon_hrJion.png",

?children: [

?{ id: 122, name: "招聘申請(qǐng)", path: "/Hr-index/zpApply" },

?{ id: 123, name: "招聘審核", path: "/Hr-index/zpCheck" },

?{ id: 124, name: "結(jié)果統(tǒng)計(jì)", path: "/Hr-index/resultInfo" }

?]

?},

?{

?id: 10,

?name: "解聘信息",

?path: "",

?icon: "icon_hrunBind.png",

?children: [

?{ id: 119, name: "解聘信息", path: "/Hr-index/decruitment" },

?{ id: 121, name: "解聘教師端", path: "/Hr-index/jpteacher" }

?]

?},

?{

?id: 3,

?name: "勞資管理",

?path: "",

?icon: "icon_hrLabour.png",

?children: [

?{ id: 117, name: "勞資詳情", path: "/Hr-index/labour/details" },

?{

?id: 118,

?name: "崗位勞資設(shè)定",

?path: "/Hr-index/labour/setting"

?}

?]

?},

?{

?id: 4,

?name: "考勤管理",

?path: "/Hr-index/check/manage",

?icon: "icon_manage.png"

?},

?{

?id: "5",

?name: "培訓(xùn)管理",

?path: "/Hr-index/modlifyift",

?icon: "icon_modifyift.png"

?},

?{

?id: "6",

?name: "離崗創(chuàng)業(yè)管理",

?path: "/Hr-index/Quit",

?icon: "icon_dimission.png"

?},

?{

?id: "7",

?name: "護(hù)照管理",

?icon: "icon_Passport.png",

?path: "/Hr-index/Passport"

?},

?{

?id: "8",

?name: "權(quán)限管理",

?icon: "icon_jurisdiction_li.png",

?path: "",

?children: [

?{ id: "115", name: "人員管理", path: "/Hr-index/Jurisdiction_set" },

?{ id: "116", name: "角色管理", path: "/Hr-index/Jurisdiction_role" }

?]

?},

?{

?id: "11",

?name: "院系專業(yè)管理",

?path: "/Hr-index/yxhome",

?icon: "icon_yxhome.png"

?},

?{

?id: "",

?name: "課程管理",

?path: "/Hr-index/KcAdministration",

?icon: "icon_KcAdministration.png"

?},

?{

?id: "9",

?name: "個(gè)人信息",

?icon: "icon_Personal_li.png",

?path: "",

?children: [

?{

?id: "111",

?name: "個(gè)人檔案rerwerwerwerwerwerwerwerwerwerwerwerwerwerwerwer",

?path: "/Hr-index/Personal_file"

?},

?{ id: "112", name: "個(gè)人考勤", path: "/Hr-index/Personal_check" },

?{ id: "113", name: "勞資詳情", path: "/Hr-index/Personal_labour" },

?{ id: "114", name: "培訓(xùn)詳情", path: "/Hr-index/Personal_train" }

?]

?}

], //導(dǎo)航數(shù)組

子組件:


html:

<div class="menutree" :class="[calssName]">

<div class>

<label v-for="menu in data" :key="menu.index">

<el-submenu :index="String(menu.id)" v-if="menu.children!=''">

<!-- <el-submenu :index="String(menu.id)" v-if="menu.children"> -->

<template slot="title">

<img :src="getImgUrl(menu.icon)" alt v-if="menu.icon" />

<span>{{menu.name}}</span>

</template>

<label>

<menutree :data="menu.children" :calssName="calssName"></menutree>

</label>

</el-submenu>

<el-menu-item :index="menu.path" v-else>

<img :src="getImgUrl(menu.icon)" alt v-if="menu.icon" />

<span

style="display:block;width:4px;height:4px;background:#ffffff;margin-top:28px;margin-left:30px; border-radius:100%;"

:style="{backgroundColor:calssName=='listtxt'?'#a6160a':'#ffffff',}"

v-else

></span>

<span

slot="title"

style="display:block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;white-space : nowrap;"

>{{menu.name}}</span>

</el-menu-item>

</label>

</div>

</div>



script:


import menutree from "./boss";

export default {

name: "menutree",

data() {

return {

menu_data: {},

name: "one"

};

},

components: {

menutree: menutree

},

props: ["data","calssName"],

mounted() {

// console.log(this.$route);

},

methods: {

getImgUrl(icon) {

return require("@/assets/" + icon);

}

}

};



css:


.menutree /deep/ .el-menu-item,

.menutree /deep/ .el-submenu__title {

color: #ffffff !important;

height: 60px;

line-height: 60px;

display: flex;

text-align: left;

padding: 0;

img {

width: 16px;

height: 16px;

float: left;

margin-top: 21px;

margin-left: 39px;

}

span {

float: left;

margin-left: 12px;

}

}

.one /deep/ .el-menu {

background: none;

}

.listtxt.menutree /deep/ .el-menu-item,.listtxt.menutree /deep/ .el-submenu__title {

color: #a6160a !important;

}


好啦,就到這里了,有不明白的地方可以跟我留言,我是前端菜雞兒,嘻嘻嘻嘻。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容