【Vue3】插槽的使用及其分类

历史小剧场

后来我才明白,造反的宋江,和招安的宋江,始终是同一个人。
为什么要造反?
造反,就是为了招安。 ----《明朝那些事儿》

概念

在日常的项目开发中,当我们在编写一个完整的组件时,不可避免的会引用一些外部组件或者自定义组件。而有些内容是在父组件中一样的,而在子组件中会对一些数据单独的处理。为了解决类似这样的问题,Vue设计出来了slot这个东西,也可以称为Vue的内容分发机制,它的主要作用就是向子组件的指定位置插入一断内容,这个内容可以是HTML或者其他组件。

默认插槽

这里有两个组件

Father

<!--  -->
<template>
    <div class="category">
        <Category title="今日美食推荐">
            <ul>
                <li v-for="item in foods" :key="item.id">{{ item.name }}</li>
            </ul>
        </Category>
        <Category title="今日美景大赛">
            <img :src="imageUrl" alt="">
        </Category>
        <Category title="今日电影直播">
            <video :src="videoUrl" controls />
        </Category>
    </div>
</template>

<script setup lang="ts" name="Father">
import { reactive, ref } from 'vue';
import Category from './Category.vue'
const foods = reactive([
    {
        id: 1,
        name: '鱼香肉丝'
    },
    {
        id: 2,
        name: '麻婆豆腐'
    },
    {
        id: 3,
        name: '大肉饼'
    }
])

const imageUrl = ref('https://images.wallpaperscraft.com/image/single/japanese_apricot_flowers_buds_1252465_1280x720.jpg')
const videoUrl = ref('https://cdn.pixabay.com/video/2021/04/12/70796-538877060_large.mp4')
</script>

<style lang="scss" scoped>
.category {
    display: flex;
    // 均分开
    justify-content: space-evenly; 
}
img, video {
    width: 100%;
}
</style>

Child:

<!--  -->
<template>
    <div class="content">
        <h2>{{ title }}</h2>
        <slot>默认内容</slot>
    </div>
</template>

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

<style lang="scss" scoped>
.content {
    background-color: skyblue;
    width: 200px;
    height: 300px;
    border-radius: 10px;
    padding: 10px;
}
h2 {
    text-align: center;
    background-color: green;
    color: white;
    font-weight: 800;
}
</style>

具名插槽

具名插槽就是给我们的插槽起一个名字,即给插槽定义一个name属性

Child:

<!--  -->
<template>
    <div class="content">
        <slot name="s1">内容1</slot>
        <slot name="s2">内容2</slot>
    </div>
</template>

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

<style lang="scss" scoped>
.content {
    background-color: skyblue;
    width: 200px;
    height: 300px;
    border-radius: 10px;
    padding: 10px;
}

</style>

Father:

<!--  -->
<template>
    <div class="category">
        <Category title="">
            <template v-slot:s2>
                <ul>
                    <li v-for="item in foods" :key="item.id">{{ item.name }}</li>
                </ul>
            </template>
            <template v-slot:s1>
                <h2>今日美食推荐</h2>
            </template>
        </Category>
        <Category>
            <template v-slot:s2>
                <img :src="imageUrl" alt="" />
            </template>
            <template v-slot:s1>
                <h2>今日美景大赛</h2>
            </template>
        </Category>
        <Category>
            <template #s2>
                <video :src="videoUrl" controls></video>
            </template>
            <template #s1>
                <h2>今日电影直播</h2>
            </template>
        </Category>
    </div>
</template>

<script setup lang="ts" name="Father">
import { reactive, ref } from 'vue';
import Category from './Category.vue'
const foods = reactive([
    {
        id: 1,
        name: '鱼香肉丝'
    },
    {
        id: 2,
        name: '麻婆豆腐'
    },
    {
        id: 3,
        name: '大肉饼'
    }
])

const imageUrl = ref('https://images.wallpaperscraft.com/image/single/japanese_apricot_flowers_buds_1252465_1280x720.jpg')
const videoUrl = ref('https://cdn.pixabay.com/video/2021/04/12/70796-538877060_large.mp4')
</script>

<style lang="scss" scoped>
.category {
    display: flex;
    // 均分开
    justify-content: space-evenly; 
}
h2 {
    text-align: center;
    background-color: green;
    color: white;
    font-weight: 800;
}
img, video {
    width: 100%;
}
</style>

作用域插槽

作用域插槽:数据在子组件那边,但根据数据生成的结构,却由父组件决定

父组件使用:变量 传递数据

<!--  -->
<template>
    <div class="content">
        <slot name="s1">内容1</slot>
        <slot name="s2" :foods="foods">内容2</slot>
    </div>
</template>

<script setup lang="ts" name="Category">
import { reactive } from 'vue';

const foods = reactive([
    {
        id: 1,
        name: '鱼香肉丝'
    },
    {
        id: 2,
        name: '麻婆豆腐'
    },
    {
        id: 3,
        name: '大肉饼'
    }
])
</script>

<style lang="scss" scoped>
.content {
    background-color: skyblue;
    width: 200px;
    height: 300px;
    border-radius: 10px;
    padding: 10px;
}

</style>

父组件可以使用params读取到子组件传递过来的数据

<!--  -->
<template>
    <div class="category">
        <Category title="">
            <template v-slot:s2="params">
                <ul>
                    <li v-for="item in params.foods" :key="item.id">{{ item.name }}</li>
                </ul>
            </template>
            <template v-slot:s1>
                <h2>今日美食推荐</h2>
            </template>
        </Category>
        <Category>
            <!-- 解构出来 -->
            <template v-slot:s2="{foods}">
                <ol>
                    <li v-for="item in foods" :key="item.id">{{ item.name }}</li>
                </ol>
            </template>
            <template v-slot:s1>
                <h2>今日美景大赛</h2>
            </template>
        </Category>
        <Category>
            <!-- 解构出来 -->
            <template #s2="{foods}">
                <h4 v-for="item in foods" :key="item.id">{{ item.name }}</h4>
            </template>
            <template #s1>
                <h2>今日电影直播</h2>
            </template>
        </Category>
    </div>
</template>

<script setup lang="ts" name="Father">
import Category from './Category.vue'


</script>

<style lang="scss" scoped>
.category {
    display: flex;
    // 均分开
    justify-content: space-evenly; 
}
h2 {
    text-align: center;
    background-color: green;
    color: white;
    font-weight: 800;
}
img, video {
    width: 100%;
}
</style>

在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值