跨组件插槽|动态插槽|公共插槽|插槽嵌套

输出

在这里插入图片描述

图解

在这里插入图片描述

代码

outlayer.vue

<template>
  <div class="unit">
    <table border="1">
      innerlayer
      <template v-for="item in slotList" :key="item.name">
        <slot :name="item.name">
          <h6>{{ item.name }}</h6>
        </slot>
      </template>
    </table>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { SlotList } from './types'

export default defineComponent({
  name: 'unit',
  props: {
    slotList: {
      type: Array as PropType<SlotList[]>,
      required: true
    }
  },
  components: {},
  setup() {
    return {}
  }
})
</script>

<style scoped></style>

middle.vue

<template>
  <div class="middle">
    <table border="1">
      middlelayer
      <unit :slotList="slotList">
        <!-- 固定插槽:一般我们把一些公共的插槽放在这里 -->
        <template #slotA>
          <h6>固定插槽|公共插槽:这是从middle.vue传递过来的插槽内容A</h6>
        </template>
        <template #slotC>
          <h6>固定插槽|公共插槽:这是从middle.vue传递过来的插槽内容C</h6>
        </template>
        <!-- 跨组件插槽 | 插槽中嵌套插槽: 这里的插槽是不同组件的插槽变化 -->
        <template v-for="item in dynamicSlotList" :key="item.name" #[item.name]>
          <slot :name="item.name"></slot>
        </template>
      </unit>
    </table>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { SlotList } from './types'
import Unit from './unit.vue'

export default defineComponent({
  name: 'middle',
  props: {
    slotList: {
      type: Array as PropType<SlotList[]>,
      required: true
    }
  },
  components: {
    Unit
  },
  setup(props) {
    const dynamicSlotList: SlotList[] = props.slotList?.filter(
      (item: SlotList) => {
        return item.isFixed ? false : true
      }
    )
    return {
      dynamicSlotList
    }
  }
})
</script>

<style scoped></style>

inner.vue

<template>
  <div class="unit">
    <table border="1">
      innerlayer
      <template v-for="item in slotList" :key="item.name">
        <slot :name="item.name">
          <h6>{{ item.name }}</h6>
        </slot>
      </template>
    </table>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { SlotList } from './types'

export default defineComponent({
  name: 'unit',
  props: {
    slotList: {
      type: Array as PropType<SlotList[]>,
      required: true
    }
  },
  components: {},
  setup() {
    return {}
  }
})
</script>

<style scoped></style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值