欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3+elementui-plus實(shí)現(xiàn)無限遞歸菜單示例代碼

 更新時(shí)間:2024年04月25日 09:35:56   作者:青蒔吖  
這篇文章主要介紹了vue3+elementui-plus實(shí)現(xiàn)無限遞歸菜單,當(dāng)一個(gè)組件的 key 值發(fā)生變化時(shí),Vue 會(huì)認(rèn)為這是一個(gè)新的組件實(shí)例,會(huì)強(qiáng)制重新創(chuàng)建和渲染這個(gè)組件,本文通過示例代碼詳細(xì)講解,需要的朋友可以參考下

效果圖

實(shí)現(xiàn)方式是:通過給定的數(shù)據(jù)結(jié)構(gòu)層數(shù)來動(dòng)態(tài)生成多級(jí)菜單

menu.vue
<template>
  <el-menu
      :default-active="activeIndex"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
      background-color="#f8f8f9"
      style="margin-top: 20px;margin-left: 1px;"
  >
    <Childrenmenu v-for="menuItem in menuItems" :key="menuItem.value" :item="menuItem" />
  </el-menu>
</template>
<script setup>
import Childrenmenu from "./childrenmenu";
const menuItems = [
	  {
	    value: '1',
	    label: '菜單1',
	    children: [
	      {
	        value: '1-1',
	        label: '子菜單1-1',
	        children: [
	          { value: '1-1-1', label: '子菜單1-1-1' },
	          { value: '1-1-2', label: '子菜單1-1-2' },
	        ],
	      },
	      { value: '1-2', label: '子菜單1-2' },
	    ],
	  },
	  {
	    value: '2',
	    label: '菜單2',
	    children: [
	      { value: '2-1', label: '子菜單2-1' },
	      {
	        value: '2-2',
	        label: '子菜單2-2',
	        children: [
	          { value: '2-2-1', label: '子菜單2-2-1' },
	          { value: '2-2-2', label: '子菜單2-2-2' },
	        ],
	      },
	    ],
	  },
	  {
	    value: '3',
	    label: '菜單3',
	    children: [
	      {
	        value: '3-1',
	        label: '子菜單3-1',
	        children: [
	          {
	            value: '3-1-1',
	            label: '子菜單3-1-1',
	            children: [
	              { value: '3-1-1-1', label: '子菜單3-1-1-1' },
	              { value: '3-1-1-2', label: '子菜單3-1-1-2' },
	            ],
	          },
	        ],
	      },
	    ],
	  },
	];
const handleSelect = async (key, keyPath) => {
	 console.log(key, keyPath)
}
</script>
childrenmenu.vue
<template>
  <template v-if="item.children">
    <el-sub-menu :index="item.value">
      <template #title>{{ item.label }}</template>
      <Childrenmenu v-for="childItem in item.children" :key="childItem.value" :item="childItem" />
    </el-sub-menu>
  </template>
  <template v-else>
    <el-menu-item :index="item.value">{{ item.label }}</el-menu-item>
  </template>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps(['item']);
</script>
<style scoped>
</style>

下面的方法可以實(shí)現(xiàn)重置菜單選項(xiàng)為默認(rèn)項(xiàng)(需求場景:左側(cè)菜單切換時(shí),上方菜單就要重置為默認(rèn)選項(xiàng))

<el-menu
      :key="menuKey"
      :default-active="activeIndex"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
      background-color="#f8f8f9"
      style="margin-left: 1px;"
  >
    <Childrenmenu v-for="menuItem in menuItems" :key="menuItem.value" :item="menuItem" />
  </el-menu>
<script setup>
const menuKey = ref(0);
//監(jiān)聽切換事件
watch(() => stationsStore.stationId, (newValue, oldValue) => {
  menuKey.value += 1;
});

通過給el-menu添加:key="menuKey"實(shí)現(xiàn)。
實(shí)現(xiàn)原理::key=“menuKey” 是 Vue 中的一個(gè)特殊語法,用于控制組件的重新渲染。當(dāng)一個(gè)組件的 key 值發(fā)生變化時(shí),Vue 會(huì)認(rèn)為這是一個(gè)新的組件實(shí)例,會(huì)強(qiáng)制重新創(chuàng)建和渲染這個(gè)組件。

到此這篇關(guān)于vue3+elementui-plus實(shí)現(xiàn)無限遞歸菜單的文章就介紹到這了,更多相關(guān)vue3 elementui-plus遞歸菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue實(shí)現(xiàn)模糊查詢filter()實(shí)例詳解

    Vue實(shí)現(xiàn)模糊查詢filter()實(shí)例詳解

    因?yàn)榻赵趯W(xué)習(xí)并使用VUE,客戶有一個(gè)要求,要輸入框可模糊查詢并帶有下拉提示的應(yīng)用,數(shù)據(jù)從接口取,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)模糊查詢filter()的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • vue中子組件向父組件傳遞數(shù)據(jù)的實(shí)例代碼(實(shí)現(xiàn)加減功能)

    vue中子組件向父組件傳遞數(shù)據(jù)的實(shí)例代碼(實(shí)現(xiàn)加減功能)

    這篇文章主要介紹了vue中子組件向父組件傳遞數(shù)據(jù)的實(shí)例代碼(實(shí)現(xiàn)加減功能) ,需要的朋友可以參考下
    2018-04-04
  • vue自定義翻頁組件的方法

    vue自定義翻頁組件的方法

    這篇文章主要為大家詳細(xì)介紹了vue自定義翻頁組件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié))

    ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié))

    這篇文章主要介紹了ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Vue響應(yīng)式原理深入分析

    Vue響應(yīng)式原理深入分析

    響應(yīng)式就是當(dāng)對(duì)象本身(對(duì)象的增刪值)或者對(duì)象屬性(重新賦值)發(fā)生變化時(shí),將會(huì)運(yùn)行一些函數(shù),最常見的就是render函數(shù),下面這篇文章主要給大家介紹了關(guān)于Vue3響應(yīng)式原理的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Vue自定義指令詳解

    Vue自定義指令詳解

    這篇文章主要為大家詳細(xì)介紹了Vue自定義指令的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue.js使用v-if實(shí)現(xiàn)顯示與隱藏功能示例

    vue.js使用v-if實(shí)現(xiàn)顯示與隱藏功能示例

    這篇文章主要介紹了vue.js使用v-if實(shí)現(xiàn)顯示與隱藏功能,結(jié)合簡單實(shí)例形式分析了使用v-if進(jìn)行判斷實(shí)現(xiàn)元素的顯示與隱藏功能,需要的朋友可以參考下
    2018-07-07
  • webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟

    webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟

    這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時(shí)候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下
    2023-11-11
  • vue3實(shí)現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法

    vue3實(shí)現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法

    這篇文章主要給大家介紹了關(guān)于vue3實(shí)現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法,store是一個(gè)狀態(tài)管理工具,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • Vue Router之router.push和router.resolve頁面跳轉(zhuǎn)方式

    Vue Router之router.push和router.resolve頁面跳轉(zhuǎn)方式

    這篇文章主要介紹了Vue Router之router.push和router.resolve頁面跳轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04

最新評(píng)論