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>
相关推荐
阳火锅几秒前
老板要周报?我说不用,代码提交就是进度
前端·javascript·vue.js
chjif1 分钟前
一张照片和一段声音如何生成口播视频:MiniMax H3 音画同步实测
java·前端·javascript
lucky九年2 分钟前
deepseek harness主题插件dsh-web-theme
前端
FungLeo8 分钟前
成为全栈·Node 后端篇·后端工程从零搭建:TypeScript、目录与热更新
javascript·typescript·node 后端
2601_962203519 分钟前
【SpringBoot3】面向切面 AspectJ AOP 使用详解
开发语言·前端·python
XLYcmy37 分钟前
HTML/CSS/JS 基础与 Vue 技术栈深度解析
java·前端·css·html·vue3·vue2·api
新知图书1 小时前
12.3 智能体中Skill的约束与自进化实战
java·前端·数据库
风月说与山鬼1 小时前
四、Tailwind CSS——间距与尺寸
前端·css·tailwind css
en.en..1 小时前
C语言 static函数与头文件封装规范
java·c语言·前端
计算机魔术师1 小时前
旅途中用手机远程控制电脑?电脑不在身边,ToDesk/UU远程/TeamViewer移动端深度实测
前端