最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3?通過作用域插槽實(shí)現(xiàn)樹形菜單嵌套組件

 更新時(shí)間:2023年01月19日 11:09:49   作者:SoaringHeart  
這篇文章主要為大家介紹了Vue3?通過作用域插槽實(shí)現(xiàn)樹形菜單嵌套組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、需求來源

工作中需要一種樹形菜單組件,經(jīng)過兩天的構(gòu)思最終通過作用域插槽實(shí)現(xiàn): 此組件將每個(gè)節(jié)點(diǎn)(插槽名為 node)暴露出來。

通過插槽的 attributes 向當(dāng)前插槽節(jié)點(diǎn)傳遞子項(xiàng) item(數(shù)據(jù)對(duì)象)和level(層深)參數(shù),在保持組件內(nèi)部極簡的同時(shí)支持在數(shù)據(jù)模型中擴(kuò)展性?;具_(dá)到比較好的封裝顆粒度,大家可以在此基礎(chǔ)上無限擴(kuò)展封裝具體的業(yè)務(wù)邏輯。

二、效果圖

let list = reactive(
  [{ 
    name:'1 一級(jí)菜單',
    isExpand: true,//是否展開子項(xiàng)
    enabled: false,//是否可以響應(yīng)事件
    child:[
      { name:'1.1 二級(jí)菜單',     
        isExpand: true,
        child:[
          { name:'1.1.1 三級(jí)菜單', isExpand: true, },
        ]
      },
      { name:'1.2 二級(jí)菜單', isExpand: true, },
    ]
  },
  { 
    name:'1.1 一級(jí)菜單',
    isExpand: true,
    child:[
      { name:'1.1.1 二級(jí)菜單', isExpand: true, },
      { name:'1.1.2 二級(jí)菜單', 
        isExpand: false, 
        child:[
          { name:'1.1.2.1 三級(jí)菜單', isExpand: true, },
        ]},
    ]
  },]
);

三、使用示例(VTreeNodeDemo.vue)

<template>
  <VTreeNode 
    :list="list"
    :level="level"
  >
    <template #node="slotProps">
      <div class="tree-node">
        {{prefix(slotProps.level)}}{{slotProps.item.name}}{{sufix(slotProps.item)}}
      </div>
    </template>
  </VTreeNode>
</template>
<script setup>
import VTreeNode from '@/components/VTreeNode/VTreeNode.vue';
import { ref, reactive, watch, onMounted, } from 'vue';
let list = reactive(
  [{ 
    name:'1 一級(jí)菜單',
    isExpand: true,//是否展開子項(xiàng)
    enabled: false,//是否可以響應(yīng)事件
    child:[
      { name:'1.1 二級(jí)菜單',     
        isExpand: true,
        child:[
          { name:'1.1.1 三級(jí)菜單', isExpand: true, },
        ]
      },
      { name:'1.2 二級(jí)菜單', isExpand: true, },
    ]
  },
  { 
    name:'1.1 一級(jí)菜單',
    isExpand: true,
    child:[
      { name:'1.1.1 二級(jí)菜單', isExpand: true, },
      { name:'1.1.2 二級(jí)菜單', 
        isExpand: false, 
        child:[
          { name:'1.1.2.1 三級(jí)菜單', isExpand: true, },
        ]},
    ]
  },]
);
const level = ref(0);
const prefix = (count) => {
  return '__'.repeat(count);
};
const sufix = (item) => {
  if (!Reflect.has(item, 'child')) {
    return '';
  }
  return ` (${item.child.length}子項(xiàng))`;
};
</script>
<style scoped lang='scss'>
.tree-node{
  height: 45px;
  display: flex;
  justify-self: center;
  align-items: center;
  // background-color: green;
  border-bottom: 1px solid #e4e4e4;
}
</style>

四、源碼(VTreeNode.vue):

<template>
  <!-- <div> -->
    <div v-for="(item,index) in list" :key="index">
      <slot name="node" :item="item" :level="levelRef">
        <div>{{ item.name }}</div>
      </slot>
      <div v-show="item.child && canExpand(item)" >
        <VTreeNode :list="item.child" :level="levelRef">
          <template #node="slotProps">
            <slot name="node" :item="slotProps.item" :level="slotProps.level">
              <div>{{ slotProps.item.name }}</div>
            </slot>
          </template>
        </VTreeNode>
      </div>
    </div>
  <!-- </div> -->
</template>
<script setup>
import { ref, reactive, watch, computed, onMounted, } from 'vue';
const props = defineProps({
  list: {
    type: Array,
    default: () => [],
    validator: (val) => {
      return Array.isArray(val) && val.every(e => Reflect.has(e, 'name'));
    }
  },
  level: {
    type: Number,
    default: 0,
  }
});
const emit = defineEmits(['update:level', ])
const levelRef = computed({
  set: (newVal) => {
    if (props.level !== newVal) {
      emit("update:level", newVal);
    }
  },
  get: () => {
    const tmp = props.level + 1;
    return tmp;
  },
});
const canExpand = (item) => {
  return Reflect.has(item, 'isExpand') && item.isExpand;
};
// onMounted(() => {
//   console.log(`levelRef:${levelRef.value}`);
// });
</script>

VTreeNode.vue

VTreeNodeDemo.vue

以上就是Vue3 通過作用域插槽實(shí)現(xiàn)樹形菜單/嵌套組件的詳細(xì)內(nèi)容,更多關(guān)于Vue3 樹形菜單嵌套組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Vue中實(shí)現(xiàn)視頻播放的示例詳解

    Vue中實(shí)現(xiàn)視頻播放的示例詳解

    這篇文章主要為大家學(xué)習(xí)介紹了基于Vue如何實(shí)現(xiàn)視頻播放的功能,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的小伙伴可以了解一下
    2023-08-08
  • VUE.CLI4.0配置多頁面入口的實(shí)現(xiàn)

    VUE.CLI4.0配置多頁面入口的實(shí)現(xiàn)

    這篇文章主要介紹了VUE.CLI4.0配置多頁面入口的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Vue數(shù)組更新及過濾排序功能

    Vue數(shù)組更新及過濾排序功能

    Vue為了增加列表渲染的功能,增加了一組觀察數(shù)組的方法,而且可以顯示一個(gè)數(shù)組的過濾或排序的副本。本文將詳細(xì)介紹Vue數(shù)組更新及過濾排序
    2017-08-08
  • Vue 組件注冊(cè)實(shí)例詳解

    Vue 組件注冊(cè)實(shí)例詳解

    這篇文章主要介紹了Vue 組件注冊(cè),結(jié)合實(shí)例形式較為詳細(xì)的分析了vue.js組件的常見分類、注冊(cè)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-02-02
  • vue3 el-pagination 將組件中英文‘goto’ 修改 為 中文到‘第幾’

    vue3 el-pagination 將組件中英文‘goto’ 修改 為&nbs

    這篇文章主要介紹了vue3 el-pagination 將組件中英文‘goto’ 修改 為 中文到‘第幾’,通過實(shí)例代碼介紹了vue3項(xiàng)目之Pagination 組件,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)5

    Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)5

    這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • vue雙向數(shù)據(jù)綁定知識(shí)點(diǎn)總結(jié)

    vue雙向數(shù)據(jù)綁定知識(shí)點(diǎn)總結(jié)

    這篇文章主要介紹了vue雙向數(shù)據(jù)綁定的原理以及知識(shí)點(diǎn)總結(jié),并做了代碼實(shí)例分析,有需要的朋友參考下。
    2018-04-04
  • Vue.js中集成Socket.IO實(shí)現(xiàn)實(shí)時(shí)聊天功能

    Vue.js中集成Socket.IO實(shí)現(xiàn)實(shí)時(shí)聊天功能

    隨著 Web 技術(shù)的發(fā)展,實(shí)時(shí)通信成為現(xiàn)代 Web 應(yīng)用的重要組成部分,Socket.IO 是一個(gè)流行的庫,支持及時(shí)、雙向與基于事件的通信,適用于各種平臺(tái)和設(shè)備,本文將介紹如何在 Vue.js 項(xiàng)目中集成 Socket.IO,實(shí)現(xiàn)一個(gè)簡單的實(shí)時(shí)聊天應(yīng)用,需要的朋友可以參考下
    2024-12-12
  • 關(guān)于vue父組件調(diào)用子組件的方法

    關(guān)于vue父組件調(diào)用子組件的方法

    本文主要介紹了vue父組件調(diào)用子組件的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 解決nuxt 自定義全局方法,全局屬性,全局變量的問題

    解決nuxt 自定義全局方法,全局屬性,全局變量的問題

    這篇文章主要介紹了解決nuxt 自定義全局方法,全局屬性,全局變量的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11

最新評(píng)論

凤山县| 吴桥县| 博客| 遂宁市| 伊宁县| 荔浦县| 锡林郭勒盟| 名山县| 麦盖提县| 南汇区| 张家川| 大田县| 土默特左旗| 喀什市| 临洮县| 江孜县| 彭山县| 林州市| 集贤县| 孟村| 元阳县| 马龙县| 平潭县| 蒙自县| 乃东县| 鸡泽县| 灌南县| 辉县市| 九龙县| 轮台县| 安阳市| 内江市| 江油市| 东莞市| 平昌县| 城固县| 广河县| 石林| 崇信县| 抚松县| 松江区|