Vue實(shí)現(xiàn)嵌套菜單組件
本案例為大家分享了Vue實(shí)現(xiàn)嵌套菜單組件的具體代碼,供大家參考,具體內(nèi)容如下
本文旨在使用Vue.js完成一個(gè)嵌套的菜單組件,使用mock.js對(duì)數(shù)據(jù)進(jìn)行模擬,并且最小化復(fù)現(xiàn)功能

安裝mock
cnpm i mockjs
//引入mockjs
import Mock from 'mockjs'
//使用mockjs模擬數(shù)據(jù)
Mock.mock('/menuData', 'get', {
? ? "ret": 0,
? ? "data": [{
? ? ? ? ? ? "id": 1,
? ? ? ? ? ? "name": "第一層",
? ? ? ? ? ? "children": [{
? ? ? ? ? ? ? ? ? ? "name": "第二層"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? "name": "第二層"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? "name": "第二層"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? ? "id": 2,
? ? ? ? ? ? "name": "第一層",
? ? ? ? ? ? "children": [{
? ? ? ? ? ? ? ? ? ? "name": "第二層"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? "id": 3,
? ? ? ? ? ? ? ? ? ? "name": "第二層",
? ? ? ? ? ? ? ? ? ? "children": [{
? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第三層"
? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第三層"
? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第三層"
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? "id": 4,
? ? ? ? ? ? ? ? ? ? "name": "第二層",
? ? ? ? ? ? ? ? ? ? "children": [{
? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第三層"
? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第三層"
? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? "id": 5,
? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第三層",
? ? ? ? ? ? ? ? ? ? ? ? ? ? "children": [{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第四層"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第四層"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "id": 6,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第四層",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "children": [{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第五層"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第五層"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "name": "第五層"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? }
? ? ]
});菜單組件
<!--
?* @Author: byron
?* @Date: 2022-02-24 09:01:27
?* @LastEditTime: 2022-02-24 10:50:38
-->
<template>
? ? <div>
? ? ? ? <ul class="menu" v-for="item in data" :key="item.id">
? ? ? ? ? ? <li
? ? ? ? ? ? ? ? class="subMenu"
? ? ? ? ? ? ? ? @click="showhd(item)"
? ? ? ? ? ? ? ? :class="[item.children ? 'color' : '']"
? ? ? ? ? ? >
? ? ? ? ? ? ? ? <span> {{ item.name }} {{ item.id }}</span>
? ? ? ? ? ? </li>
? ? ? ? ? ? <div class="child" v-if="item.id == currentId && openFlag">
? ? ? ? ? ? ? ? <xxxx v-if="item.children" :data="item.children"></xxxx>
? ? ? ? ? ? </div>
? ? ? ? </ul>
? ? </div>
</template>
<script>
export default {
? ? name: 'xxxx',
? ? components: {},
? ? props: ['data'],
? ? data() {
? ? ? ? return {
? ? ? ? ? ? currentId: undefined,
? ? ? ? ? ? openFlag: false,
? ? ? ? }
? ? },
? ? methods: {
? ? ? ? showhd(a) {
? ? ? ? ? ? console.log(this.openFlag)
? ? ? ? ? ? this.openFlag = !this.openFlag
? ? ? ? ? ? this.currentId = a.id
? ? ? ? ? ? console.log(this.currentId)
? ? ? ? ? ? console.log(this.openFlag)
? ? ? ? },
? ? },
}
</script>
<style scoped>
li {
? ? text-decoration: none;
? ? /* list-style: none; */
}
.ul {
? ? overflow: hidden;
}
.head {
? ? display: none;
}
.show {
? ? display: block;
}
.color {
? ? color: brown;
}
</style>使用菜單組件
<template>
? ? <div id="app">
? ? ? ? <h1>嵌套組件的實(shí)現(xiàn)</h1>
? ? ? ? <HelloWorld :data="menu" />
? ? </div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import axios from 'axios'
export default {
? ? name: 'App',
? ? components: {
? ? ? ? HelloWorld,
? ? },
? ? data() {
? ? ? ? return {
? ? ? ? ? ? menu: [],
? ? ? ? }
? ? },
? ? async created() {
? ? ? ? const { data: r } = await axios.get('/menuData')
? ? ? ? this.menu = r.data
? ? ? ? console.log(this.menu)
? ? },
}
</script>
<style>
#app {
? ? font-family: Avenir, Helvetica, Arial, sans-serif;
? ? -webkit-font-smoothing: antialiased;
? ? -moz-osx-font-smoothing: grayscale;
? ? text-align: center;
? ? color: #2c3e50;
? ? margin-top: 60px;
}
</style>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue3+TypeScript實(shí)現(xiàn)遞歸菜單組件的完整實(shí)例
- 原生Vue 實(shí)現(xiàn)右鍵菜單組件功能
- vue iview的菜單組件Mune 點(diǎn)擊不高亮的解決方案
- Vue.js下拉菜單組件使用方法詳解
- vue下拉菜單組件(含搜索)的實(shí)現(xiàn)代碼
- vue移動(dòng)端UI框架實(shí)現(xiàn)QQ側(cè)邊菜單組件
- vuejs實(shí)現(xiàn)遞歸樹(shù)型菜單組件
- Vue側(cè)滑菜單組件——DrawerLayout
- Vue.js手風(fēng)琴菜單組件開(kāi)發(fā)實(shí)例
- vue多級(jí)多選菜單組件開(kāi)發(fā)
相關(guān)文章
vue實(shí)現(xiàn)書本翻頁(yè)動(dòng)畫效果實(shí)例詳解
這篇文章主要介紹了vue簡(jiǎn)單實(shí)現(xiàn)書本翻頁(yè)效果,需要的朋友可以參考下2022-04-04
如何解決npm i下載依賴的時(shí)候出現(xiàn)某依賴版本沖突
這篇文章主要介紹了如何解決npm i 下載依賴的時(shí)候出現(xiàn)某依賴版本沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vuex頁(yè)面刷新數(shù)據(jù)丟失解決方法詳解
這篇文章主要為大家介紹了vuex頁(yè)面刷新數(shù)據(jù)丟失解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
詳解vue 路由跳轉(zhuǎn)四種方式 (帶參數(shù))
這篇文章主要介紹了vue 路由跳轉(zhuǎn)四種方式 (帶參數(shù)),本文通過(guò)實(shí)例代碼給大家介紹的詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
解決vue 給window添加和移除resize事件遇到的坑
這篇文章主要介紹了解決vue 給window添加和移除resize事件遇到的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue3+Tsx給路由加切換動(dòng)畫時(shí)的踩坑及解決
這篇文章主要介紹了Vue3+Tsx給路由加切換動(dòng)畫時(shí)的踩坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
詳解vue2父組件傳遞props異步數(shù)據(jù)到子組件的問(wèn)題
本篇文章主要介紹了vue2父組件傳遞props異步數(shù)據(jù)到子組件的問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Vue中import from@符號(hào)指的是什么意思
這篇文章主要介紹了Vue中import from@符號(hào)指的是意思,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue通過(guò)getAction的finally來(lái)最大程度避免影響主數(shù)據(jù)呈現(xiàn)問(wèn)題
這篇文章主要介紹了Vue通過(guò)getAction的finally來(lái)最大程度避免影響主數(shù)據(jù)呈現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

