vue實現多級側邊欄的封裝
本文實例為大傢分享瞭vue實現多級側邊欄的封裝代碼,供大傢參考,具體內容如下
父組件
// 左側導航菜單封裝 <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: '導航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: '導航2', child: [ { name: '選項1', url: 'aaa' }, { name: '選項2', url: 'bbb' }, ] }, { name: '導航3', url: 'eee' }, { name: '導航4', } ] } }, methods: { handleLink($event,item,index){ if(item.url){ this.value = '跳轉到' + 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>
效果圖
復制粘貼即可使用
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- vue遞歸組件實現elementUI多級菜單
- Vue實現路由嵌套的方法實例
- vue.js使用Element-ui中實現導航菜單
- Vue開發Sort組件代碼詳解
- Vue中利用better-scroll組件實現橫向滾動功能