亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3 Element-plus el-menu無限級菜單組件封裝過程

 更新時間:2023年04月26日 11:25:30   作者:奧特曼  
對于element中提供給我們的el-menu組件最多可以實現(xiàn)三層嵌套,如果多一層數(shù)據(jù)只能自己通過變量去加一層,如果加了兩層、三層這種往往是行不通的,所以只能進行封裝,這篇文章主要介紹了Vue3 Element-plus el-menu無限級菜單組件封裝,需要的朋友可以參考下

對于element中提供給我們的el-menu組件最多可以實現(xiàn)三層嵌套,如果多一層數(shù)據(jù)只能自己通過變量去加一層,如果加了兩層、三層這種往往是行不通的,所以只能進行封裝

效果圖

 一、定義數(shù)據(jù)

MenuData.ts

export default [
    {
        id: "1",
        name: "第一級菜單",
        level: '1',
        child: [
            {
                id: "11",
                name: "第二級菜單",
                level: '1-1',
                child: [
                    {
                        id: "111",
                        name: "第三級菜單",
                        level: '1-1-1',
                        child: [
                            {
                                id: "1111",
                                name: "第四級菜單",
                                level: '1-1-1-1',
                                child: [
                                    {
                                        id: "11111",
                                        name: "第五級菜單",
                                        level: '1-1-1-1-1',
                                        child: []
                                    }
                                ]
                            }
                        ]
                    }]
            }
        ]
    },
    {
        id: "2",
        name: "第一級同級菜單",
        level: '2',
        child: []
    }
 
]

二、封裝組件 

封裝思想:

 1.對本身組件進行循環(huán)使用,如果有子集使用本身組件 把child數(shù)據(jù)傳給自己

 2.如果沒有子集 使用 el-menu-item

以下代碼對setup( )函數(shù)和setup語法糖分別做了實現(xiàn) 

setup語法糖

<template>
  <el-menu
    :default-active="defaultActive"
    :unique-opened="true"
    class="el-menu-vertical-demo"
  >
    <template v-for="item in menu">
      <!-- 如果有子集 -->
      <template v-if="item.child && item.child.length > 0">
        <el-sub-menu
          :key="item.id"
          :index="item.level"
          :disabled="item.meta?.disabled"
          :popper-append-to-body="false"
        >
          <template #title>
            <i :class="[item.meta?.icon]"></i>
            <!-- 添加空格 表示下級-->
            <span> {{ generateSpaces(item.level) }} </span>
            <span slot="title"> {{ item.name }}</span>
          </template>
          <MenuTree
            :menu="item.child"
            :defaultActive="defaultActive"
            @clickItem="clickItemHandle"
          />
        </el-sub-menu>
      </template>
      <!-- 如果沒有子集 -->
      <template v-else>
        <el-menu-item
          :key="item.id"
          :index="item.level"
          :disabled="item.meta?.disabled"
          :popper-append-to-body="false"
          @click="clickItemHandle(item)"
        >
          <i :class="[item.meta?.icon]"></i>
          <!-- 添加空格 表示下級-->
          <span> {{ generateSpaces(item.level) }} </span>
          <span slot="title">{{ item.name }}</span>
        </el-menu-item>
      </template>
    </template>
  </el-menu>
</template>
 
<script lang="ts" name="MenuTree" setup>
// 把下面代碼變成setup語法糖的形式 
import type { PropType } from "vue";
import type { MenuItem } from "@/types/lesson";
// type 為了方便寫成這樣 可以根據(jù)自己項目設(shè)定type
 defineProps({
  menu: {
    type: Array as unknown as PropType<any[]>,
    required: true,
    default: () => [],
  },
  defaultActive: {
    type: String as unknown as PropType<string>,
    required: true,
    default: [],
  },
});
 
const emit = defineEmits(["update-active-path", "clickItem"]);
 
// 返回的空格字符串 用于顯示菜單層級 
const generateSpaces = (level: string) => {
  let str = "";
  level.split("")  .filter((it) => it != "-") .forEach(() => {
      str += " ";
  });
  return str;
};
 
// 點擊當前菜單項
const clickItemHandle = (item: MenuItem) => {
  emit("clickItem", item);
};
</script>
 
<style scoped lang="less">
.el-menu {
  width: 288px;
}
</style>

setup函數(shù)

<template>
  <el-menu :default-active="defaultActive"  :unique-opened="true"   class="el-menu-vertical-demo"  >
    <template v-for="item in menu">
        <template v-if="item.child && item.child.length > 0">
          <el-sub-menu 
            :key="item.id"
            :index="item.level"
            :disabled="item.meta?.disabled"
            :popper-append-to-body="false"
          >
            <template #title>
              <i :class="[item.meta?.icon]"></i>
              <!-- 添加空格 表示下級-->
              <span> {{ generateSpaces(item.level) }} </span>
              <span slot="title"> {{ item.name }}</span>
            </template>
            <MenuTree :menu="item.child" :defaultActive="defaultActive"  @clickItem="clickItemHandle"  />
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item 
            :key="item.id"
            :index="item.level"
            :disabled="item.meta?.disabled"
            :popper-append-to-body="false"
            @click="clickItemHandle(item)"
          >
            <i :class="[item.meta?.icon]"></i>
            <!-- 添加空格 表示下級-->
            <span> {{ generateSpaces(item.level) }} </span>
            <span slot="title">{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
  </el-menu>
</template>
 
<script lang="ts">
import { defineComponent, toRefs } from 'vue';
import type { PropType } from 'vue'
import type {MenuItem} from '@/types/lesson'
export default defineComponent({
  name: 'MenuTree',
  props: {
    menu: {
      type: Array as unknown as PropType<any[]>,
      required: true,
      default: () => [],
    },
    defaultActive: {
      type: String as unknown as PropType<string>,
      required: true,
      default: '',
    },
  },
  emits: ['update-active-path','clickItem'],
  setup(props, context) {
    const { menu, defaultActive } = toRefs(props);
 
    const generateSpaces = (level:string) => {
      let str = ''
      level.split('').filter(it=>it!='-').forEach(() => {
        str += ' '
      })
      return str
    }
 
    const clickItemHandle = (item:MenuItem) => {
      context.emit('clickItem', item)
    }
 
   
    return {
      clickItemHandle,
      menu,
      defaultActive,
      generateSpaces,
    }
  },
});
</script>
 
<style scoped lang="less">
  .el-menu {
    width: 288px;
  }
</style>

 type就不補充了 可根據(jù)自己項目定義,可臨時改成any

三、使用組件

<template>
 <MenuTree
        :menu="menuList"
        :defaultActive="defaultActive"
        @clickItem="handleMenuClick"
        :update-click="handleMenuClick"
      />
</template>
 
<script setup lang="ts">
import MenuTree from "./components/MenuTree.vue";
import type {MenuItem} from '@/types/lesson'
import menuData from './MenuData'
 
const defaultActive = ref<string>(''); // "1-1-1-1" 默認選中的數(shù)據(jù)
const menuList = ref(menuData)
 
const handleMenuClick = (item:MenuItem) => {
  console.log('父組件',item);
};
</script>

補充default-active變量,如果一開始想默認點開第一層的數(shù)據(jù) 就需要找規(guī)律啦

拿到所有的level,通過接口方式返給你 自己平鋪拿到所有的level也好 

例如數(shù)據(jù)格式:

let arr = [  "1-1",
  "1-1-1",
  "1-1-1-1",
  "1-1-1-2",
  "1-1-1-3",
  "1-1-1-4",
  "1-1-1-5",
  "1-1-1-6",
  "1-1-2",
  "1-1-2-1"
]

 想要的結(jié)果就是 最長且相同數(shù)字最多的元素 1-1-1-1

arr.sort((a,b)=> b.split('-').length - a.split('-').length)[0]

使用split防止有些字符串是10、11 兩位數(shù)字的

到此這篇關(guān)于Vue3 Element-plus el-menu無限級菜單組件封裝的文章就介紹到這了,更多相關(guān)Vue3 Element-plus el-menu內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue2實現(xiàn)移動端上傳、預覽、壓縮圖片解決拍照旋轉(zhuǎn)問題

    vue2實現(xiàn)移動端上傳、預覽、壓縮圖片解決拍照旋轉(zhuǎn)問題

    這篇文章主要介紹了vue2實現(xiàn)移動端上傳、預覽、壓縮圖片解決拍照旋轉(zhuǎn)問題,需要的朋友可以參考下
    2017-04-04
  • VUE實現(xiàn)自身整體組件銷毀的示例代碼

    VUE實現(xiàn)自身整體組件銷毀的示例代碼

    這篇文章主要介紹了VUE實現(xiàn)自身整體組件銷毀的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • npm?run?dev失敗的簡單解決辦法

    npm?run?dev失敗的簡單解決辦法

    最近工作中遇到了npm?run?dev報錯這個問題,通過查找相關(guān)資料最終解決了,所以下面這篇文章主要給大家介紹了關(guān)于npm?run?dev失敗的詳細解決辦法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • Vue中使用sass實現(xiàn)換膚功能

    Vue中使用sass實現(xiàn)換膚功能

    這篇文章主要介紹了Vue中使用sass實現(xiàn)換膚功能,實現(xiàn)此功能用到了三個主要文件(base.scss、mixin.scss、varibale.scss),需要的朋友可以參考下
    2018-09-09
  • vue element-ui table表格滾動加載方法

    vue element-ui table表格滾動加載方法

    下面小編就為大家分享一篇vue element-ui table表格滾動加載方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Vue中Mixin的正確用法詳解

    Vue中Mixin的正確用法詳解

    眾所周知,vue 的 mixins 是個非常靈活,但又很容易帶來混亂的 API,Mixins 本該是一種強大的重用代碼的手段,但使用之后往往帶來更多的混亂,代碼變得不易維護,本文就詳細介紹Vue Mixin的正確用法,需要的朋友可以參考下
    2023-06-06
  • vue滑動解鎖組件使用方法詳解

    vue滑動解鎖組件使用方法詳解

    這篇文章主要為大家詳細介紹了vue滑動解鎖組件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • vue3之Suspense加載異步數(shù)據(jù)的使用

    vue3之Suspense加載異步數(shù)據(jù)的使用

    本文主要介紹了vue3之Suspense加載異步數(shù)據(jù)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Vue Element前端應用開發(fā)之常規(guī)的JS處理函數(shù)

    Vue Element前端應用開發(fā)之常規(guī)的JS處理函數(shù)

    在我們使用Vue Element處理界面的時候,往往碰到需要利用JS集合處理的各種方法,如Filter、Map、reduce等方法,也可以涉及到一些對象屬性賦值等常規(guī)的處理或者遞歸的處理方法,本篇隨筆列出一些在VUE+Element 前端開發(fā)中經(jīng)常碰到的JS處理場景,供參考學習。
    2021-05-05
  • 詳解用vue.js和laravel實現(xiàn)微信支付

    詳解用vue.js和laravel實現(xiàn)微信支付

    本篇文章主要介紹了用vue.js和laravel實現(xiàn)微信支付,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論