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>
相关推荐
css趣多多16 分钟前
案例自定义tabBar
前端
林的快手2 小时前
CSS列表属性
前端·javascript·css·ajax·firefox·html5·safari
匹马夕阳2 小时前
ECharts极简入门
前端·信息可视化·echarts
bug总结2 小时前
新学一个JavaScript 的 classList API
开发语言·javascript·ecmascript
网络安全-老纪2 小时前
网络安全-js安全知识点与XSS常用payloads
javascript·安全·web安全
API_technology2 小时前
电商API安全防护:JWT令牌与XSS防御实战
前端·安全·xss
yqcoder3 小时前
Express + MongoDB 实现在筛选时间段中用户名的模糊查询
java·前端·javascript
十八朵郁金香3 小时前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
计算机-秋大田3 小时前
基于Spring Boot的兴顺物流管理系统设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·spring·课程设计
GDAL4 小时前
HTML 中的 Canvas 样式设置全解
javascript