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

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

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

一、需求來源

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

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

二、效果圖

let list = reactive(
  [{ 
    name:'1 一級菜單',
    isExpand: true,//是否展開子項
    enabled: false,//是否可以響應(yīng)事件
    child:[
      { name:'1.1 二級菜單',     
        isExpand: true,
        child:[
          { name:'1.1.1 三級菜單', isExpand: true, },
        ]
      },
      { name:'1.2 二級菜單', isExpand: true, },
    ]
  },
  { 
    name:'1.1 一級菜單',
    isExpand: true,
    child:[
      { name:'1.1.1 二級菜單', isExpand: true, },
      { name:'1.1.2 二級菜單', 
        isExpand: false, 
        child:[
          { name:'1.1.2.1 三級菜單', 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 一級菜單',
    isExpand: true,//是否展開子項
    enabled: false,//是否可以響應(yīng)事件
    child:[
      { name:'1.1 二級菜單',     
        isExpand: true,
        child:[
          { name:'1.1.1 三級菜單', isExpand: true, },
        ]
      },
      { name:'1.2 二級菜單', isExpand: true, },
    ]
  },
  { 
    name:'1.1 一級菜單',
    isExpand: true,
    child:[
      { name:'1.1.1 二級菜單', isExpand: true, },
      { name:'1.1.2 二級菜單', 
        isExpand: false, 
        child:[
          { name:'1.1.2.1 三級菜單', 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}子項)`;
};
</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 通過作用域插槽實現(xiàn)樹形菜單/嵌套組件的詳細內(nèi)容,更多關(guān)于Vue3 樹形菜單嵌套組件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

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

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

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

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

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

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

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

    Vue 組件注冊實例詳解

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評論

新巴尔虎左旗| 普安县| 中超| 新晃| 济南市| 兴和县| 宁国市| 中山市| 四子王旗| 衡山县| 永年县| 西盟| 昌图县| 淮南市| 安西县| 永城市| 林周县| 松溪县| 兴城市| 永吉县| 丹棱县| 庐江县| 南汇区| 阿坝县| 中超| 工布江达县| 兴国县| 濮阳市| 广饶县| 吉木乃县| 镇宁| 额济纳旗| 博乐市| 内丘县| 赤壁市| 福建省| 南和县| 常德市| 定结县| 桐乡市| 广元市|