vue3插槽用法(默认、具名、作用域插槽)

默认插槽:

// ----------------------------------------------------父组件
<template>
  <div class="page">
    <div class="content">
      <SlotCom title="打招呼">
        <span>插槽内容:你好呀</span>
      </SlotCom>
    </div>
  </div>
</template>

<script setup lang="ts">
import SlotCom from './SlotCom.vue'
</script>

<style lang="scss" scoped>
.page {
  width: 600px;
  height: 800px;
  border: 1px solid red;
  padding: 20px;
  .content {
    width: 500px;
    height: 600px;
    background-color: antiquewhite;
    overflow: scroll;
  }
}
</style>


// ------------------------------------------------------子组件
<template>
  <div class="slot">
    <div class="title">{{ title }}</div>
    <slot>
      <span>默认内容:不穿东西过来我就展示这个</span>
    </slot>
  </div>
</template>

<script setup lang="ts">
defineProps(['title'])
</script>

<style lang="scss" scoped>
.slot {
  width: 200px;
  height: 200px;
  border-radius: 4px;
  box-shadow: 0 0 10px;
  background-color: skyblue;
}
</style>

具名插槽:

// ---------------------------------------父组件
<template>
  <div class="page">
    <div class="content">
      <!-- 写法一 -->
      <SlotCom>
        <template v-slot:content>
          <span>插槽内容:你好呀</span>
        </template>
        <template v-slot:title>
          <div>热门游戏</div>
        </template>
      </SlotCom>
      <!-- 写法二 -->
      <SlotCom>
        <template #content>
          <span>插槽内容:大漠孤鹰</span>
        </template>
        <template #title>
          <div>经典影片</div>
        </template>
      </SlotCom>
    </div>
  </div>
</template>

<script setup lang="ts">
import SlotCom from './SlotCom.vue'
</script>

<style lang="scss" scoped>
.page {
  width: 600px;
  height: 800px;
  border: 1px solid red;
  padding: 20px;
}
</style>


// ---------------------------------------------------子组件
<template>
  <div class="slot">
    <slot name="title">
      <span>默认title:不穿东西过来我就展示这个</span>
    </slot>
    <slot name="content">
      <span>默认内容:不穿东西过来我就展示这个</span>
    </slot>
  </div>
</template>

<script setup lang="ts"></script>

<style lang="scss" scoped>
.slot {
  width: 200px;
  height: 200px;
  border-radius: 4px;
  box-shadow: 0 0 10px;
  background-color: skyblue;
}
</style>

作用域插槽:

特点:数据在子组件那边,由子组件维护,但具体生成结构是由父组件决定

// --------------------------------------------------父组件
<template>
  <div class="page">
    <div class="content">
      <!-- 默认 -->
      <SlotCom>
        <template v-slot="params">
          <ol>
            <li v-for="(item, index) in params.youxi" :key="index">{{ item.name }}</li>
          </ol>
        </template>
      </SlotCom>
      <!-- 具名 -->
      <SlotCom>
        <!-- <template v-slot:name="params"> -->
        <template #name="params">
          <ol>
            <li v-for="(item, index) in params.youxi" :key="index">{{ item.name }}</li>
          </ol>
        </template>
      </SlotCom>
    </div>
  </div>
</template>

<script setup lang="ts">

import SlotCom from './SlotCom.vue'

</script>

<style lang="scss" scoped>
.page {
  width: 600px;
  height: 800px;
  border: 1px solid red;
  padding: 20px;
  .content {
    width: 500px;
    height: 600px;
    background-color: antiquewhite;
    overflow: scroll;
  }
}
</style>


//----------------------------------------------------------子组件
<template>
  <div class="slot">
    <slot :youxi="games" hh="哈哈"></slot>
    <slot name="name" :youxi="games" hh="哈哈"></slot>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
let games = reactive([{ name: '王者荣耀' }, { name: '英雄联盟' }, { name: '炉石传说' }])
</script>

<style lang="scss" scoped>
.slot {
  width: 200px;
  height: 200px;
  border-radius: 4px;
  box-shadow: 0 0 10px;
  background-color: skyblue;
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue组件的插槽是一种抽象的概念,用于分发组件的内容。Vue提供了三种类型的插槽默认插槽具名插槽作用域插槽默认插槽是没有名字的插槽,可以在组件模板中使用<slot>标签来定义。当组件没有具名插槽时,所有没有被包裹在具名插槽中的内容都会被传递到默认插槽中。 具名插槽是有名字的插槽,可以在组件模板中使用<slot name="xxx">标签来定义。当组件中有多个插槽时,可以使用具名插槽来指定要分发的内容。 作用域插槽是一种特殊的插槽,可以让父组件向子组件传递数据。作用域插槽可以在组件模板中使用<slot>标签来定义,并且可以使用一个带有参数的函数来向插槽中传递数据。 例如,下面是一个使用作用域插槽的例子: ```html <template> <div> <slot v-bind:user="user"> {{ user.lastName }} </slot> </div> </template> <script> export default { data() { return { user: { firstName: 'John', lastName: 'Doe' } } } } </script> ``` 在父组件中,可以这样使用这个组件: ```html <template> <div> <user-profile> <template v-slot:default="slotProps"> {{ slotProps.user.firstName }} </template> </user-profile> </div> </template> <script> import UserProfile from './UserProfile.vue' export default { components: { UserProfile } } </script> ``` 这个例子中,父组件向子组件传递了一个名为user的数据对象,并且在插槽中使用了一个带有参数的函数来向插槽中传递数据。在插槽中,可以使用slotProps来访问传递进来的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值