vue插槽slot用法

父组件:

javascript 复制代码
<template>
    <div class="content">
        <dialog1>
            <!-- 第一种 -->
            <!-- <template #header>
                <div>header</div>
            </template>
            <template #default="{data, index}">
                <div>{{data.name}} -- {{data.age}} -- {{index}}</div>
            </template>
            <template #footer>
                <div>footer</div>
            </template> -->

            <!-- 第二种 动态插槽 -->
            <template #[name]>
                <div>我在哪~~~</div>
            </template>
        </dialog1>
        <button @click="btn">点击</button>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import dialog1 from './dialog1.vue';
const name = ref('header');

const btn = () => {
    name.value == 'footer' ? name.value = 'header' : name.value = 'footer';
}

</script>

<style lang="scss" scoped>

</style>

子组件:

javascript 复制代码
<template>
    <div>
        <header class="header">
            <slot name="header"></slot>
        </header>
        <main class="main">
            <div v-for="(item, index) in data">
                <slot :index="index" :data="item"></slot>
            </div>
        </main>
        <footer class="footer">
            <slot name="footer"></slot>
        </footer>
    </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
type names = {
    name: string,
    age: number
}
const data = reactive<names[]>([
    {
        name: '1',
        age: 1
    },
    {
        name: '2',
        age: 2
    },
    {
        name: '3',
        age: 3
    },
])
</script>

<style scoped>
.header {
    height: 100px;
    background-color: aqua;
}
.main {
    height: 100px;
    background-color:blanchedalmond;
}
.footer {
    height: 100px;
    background-color:blueviolet;
}
</style>
相关推荐
木易 士心3 分钟前
Nginx 基本使用和高级用法详解
运维·javascript·nginx
蒙特卡洛的随机游走12 分钟前
Spark的宽依赖与窄依赖
大数据·前端·spark
共享家952719 分钟前
QT-常用控件(多元素控件)
开发语言·前端·qt
幸运小圣19 分钟前
Iterator迭代器 【ES6】
开发语言·javascript·es6
葱头的故事21 分钟前
将传给后端的数据转换为以formData的类型传递
开发语言·前端·javascript
中微子31 分钟前
🚀 2025前端面试必考:手把手教你搞定自定义右键菜单,告别复制失败的尴尬
javascript·面试
_233332 分钟前
vue3二次封装element-plus表格,slot透传,动态slot。
前端·vue.js
jump68035 分钟前
js中数组详解
前端·面试
崽崽长肉肉39 分钟前
iOS 基于Vision.framework从图片中提取文字
前端
温宇飞44 分钟前
Web Abort API - AbortSignal 与 AbortController
前端