vue實現(xiàn)多級側(cè)邊欄的封裝
本文實例為大家分享了vue實現(xiàn)多級側(cè)邊欄的封裝代碼,供大家參考,具體內(nèi)容如下
父組件
// 左側(cè)導(dǎo)航菜單封裝 <template> ? <div class="nav"> ? ? <Menu @handleLink="handleLink" :list="list"> ? ? </Menu> ? ? <div>{{value}}</div> ? </div> </template> <script> import Menu from '@/components/Menu.vue' ? export default { ? ? components:{ ? ? ? Menu ? ? }, ? ? data(){ ? ? ? return { ? ? ? ? value: '', ? ? ? ? list: [ ? ? ? ? ? { ? ? ? ? ? ? name: '導(dǎo)航1', ? ? ? ? ? ? child: [ ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? name: '選項1', ? ? ? ? ? ? ? ? child: [ ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? name: '選項1-1', ? ? ? ? ? ? ? ? ? ? url: 'aaa-1' ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? name: '選項2-1', ? ? ? ? ? ? ? ? ? ? url: 'bbb-1' ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? name: '選項3-1', ? ? ? ? ? ? ? ? ? ? url: 'ccc-1' ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ] ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? name: '選項2', ? ? ? ? ? ? ? ? url: 'bbb' ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? name: '選項3', ? ? ? ? ? ? ? ? url: 'ccc' ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? name: '選項4', ? ? ? ? ? ? ? ? url: 'ddd' ? ? ? ? ? ? ? }, ? ? ? ? ? ? ] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: '導(dǎo)航2', ? ? ? ? ? ? child: [ ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? name: '選項1', ? ? ? ? ? ? ? ? url: 'aaa' ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? name: '選項2', ? ? ? ? ? ? ? ? url: 'bbb' ? ? ? ? ? ? ? }, ? ? ? ? ? ? ] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: '導(dǎo)航3', ? ? ? ? ? ? url: 'eee' ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: '導(dǎo)航4', ? ? ? ? ? } ? ? ? ? ] ? ? ? } ? ? }, ? ?? ? ? methods: { ? ? ? handleLink($event,item,index){ ? ? ? ? if(item.url){ ? ? ? ? ? this.value = '跳轉(zhuǎn)到' + item.url ? ? ? ? } else { ? ? ? ? ? if(item.isOpen == undefined){ ? ? ? ? ? ? this.$set(item,'isOpen', true) ? ? ? ? ? } else { ? ? ? ? ? ? item.isOpen = !item.isOpen ? ? ? ? ? } ? ? ? ? } ? ? ? } ? ? } ? } </script> <style lang="less" scoped> ? *{ ? ? margin: 0; ? ? padding: 0; ? } ? .nav{ ? ? display: flex; ? } </style>
子組件
<template> ?? ?<!-- 遞歸封裝多級菜單欄 --> ?? ?<div class="navMenu"> ?? ??? ?<div ?? ??? ??? ?v-for="(item, index) in list" ?? ??? ??? ?class="item" ?? ??? ??? ?@click.stop.prevent="handleLink($event, item, index)" ?? ??? ?> ?? ??? ??? ?<div ?? ??? ??? ??? ?class="item-cont" ?? ??? ??? ??? ?:class="{ 'is-disabled': !(item.url || item.child) }" ?? ??? ??? ?> ?? ??? ??? ??? ?{{ item.name }} ?? ??? ??? ??? ?<i class="icon" v-if="item.child"></i> ?? ??? ??? ?</div> ?? ??? ??? ?<div class="list-child" :class="{ active: !item.isOpen }" v-if="item.child"> ?? ??? ??? ??? ?<Menu ?? ??? ??? ??? ??? ?:list="item.child" ?? ??? ??? ??? ??? ?@handleLink="handleLink" ?? ??? ??? ??? ?></Menu> ?? ??? ??? ?</div> ?? ??? ?</div> ?? ?</div> </template> <script> export default { ?? ?name: "Menu", ?? ?props: { ?? ??? ?list: { ?? ??? ??? ?type: Array, ?? ??? ??? ?default: [], ?? ??? ?}, ?? ?}, ?? ?methods: { ?? ??? ?handleLink($event, item, index) { ?? ??? ??? ?this.$emit("handleLink", $event, item, index); ?? ??? ?}, ?? ?}, }; </script> <style lang="less" scoped> .navMenu { ?? ?height: auto; ?? ?background: #fff; ?? ?cursor: pointer; ?? ?.item { ?? ??? ?width: 200px; ?? ??? ?color: black; ?? ??? ?.child, ?? ??? ?.item-cont { ?? ??? ??? ?min-height: 50px; ?? ??? ??? ?line-height: 50px; ?? ??? ??? ?position: relative; ?? ??? ??? ?&:hover { ?? ??? ??? ??? ?background: #94bce4; ?? ??? ??? ?} ?? ??? ??? ?&.is-disabled { ?? ??? ??? ??? ?opacity: 0.25; ?? ??? ??? ??? ?cursor: not-allowed; ?? ??? ??? ?} ?? ??? ??? ?.icon { ?? ??? ??? ??? ?content: ""; ?? ??? ??? ??? ?border: 8px solid; ?? ??? ??? ??? ?border-color: #f00 transparent transparent transparent; ?? ??? ??? ??? ?position: absolute; ?? ??? ??? ??? ?top: calc(50% - 4px); ?? ??? ??? ??? ?right: 0; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?.list-child { ?? ??? ??? ?margin-left: 30px; ?? ??? ??? ?&.active { ?? ??? ??? ??? ?display: none; ?? ??? ??? ?} ?? ??? ?} ?? ?} } </style>
效果圖
復(fù)制粘貼即可使用
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue數(shù)組中出現(xiàn)__ob__:Observer無法取值問題的解決方法
__ob__: Observer這個屬性其實是Vue監(jiān)控變量產(chǎn)生的,下面這篇文章主要給大家介紹了關(guān)于Vue數(shù)組中出現(xiàn)__ob__:?Observer無法取值問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03Element InfiniteScroll無限滾動的具體使用方法
這篇文章主要介紹了Element InfiniteScroll無限滾動的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07vue1.0和vue2.0的watch監(jiān)聽事件寫法詳解
今天小編就為大家分享一篇vue1.0和vue2.0的watch監(jiān)聽事件寫法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue.js element-ui validate中代碼不執(zhí)行問題解決方法
這篇文章主要介紹了vue.js element-ui validate中代碼不執(zhí)行問題解決方法,需要的朋友可以參考下2017-12-12Vue的Eslint配置文件eslintrc.js說明與規(guī)則介紹
最近在跟著視頻敲項目時,代碼提示出現(xiàn)很多奇奇怪怪的錯誤提示,百度了一下是eslintrc.js文件沒有配置相關(guān)命令,ESlint的語法檢測真的令人抓狂,現(xiàn)在總結(jié)一下這些命令的解釋2020-02-02在vue中實現(xiàn)給每個頁面頂部設(shè)置title
這篇文章主要介紹了在vue中實現(xiàn)給每個頁面頂部設(shè)置title,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07