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>
相关推荐
2501_946230986 小时前
Cordova&OpenHarmony通知中心实现
android·javascript
南山安6 小时前
JavaScript 函数柯里化:从入门到实战,一文搞定(面试可用)
javascript·面试·函数式编程
谢尔登6 小时前
Monorepo 架构
前端·arcgis·架构
啃火龙果的兔子6 小时前
JavaScript 中的 Symbol 特性详解
开发语言·javascript·ecmascript
栀秋6666 小时前
你会先找行还是直接拍平?两种二分策略你Pick哪个?
前端·javascript·算法
漂流瓶jz7 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·css
xhxxx7 小时前
传统工具调用太痛苦?LangChain 一键打通 LLM 与真实世界
前端·langchain·llm
南山安7 小时前
LangChain学习:Memory实战——让你的大模型记住你
前端·javascript·langchain
武昌库里写JAVA7 小时前
iview-CRUD模板
vue.js·spring boot·sql·layui·课程设计
BD_Marathon8 小时前
Promise基础语法
开发语言·前端·javascript