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>
相关推荐
一斤代码2 小时前
vue3 下载图片(标签内容可转图)
前端·javascript·vue
中微子2 小时前
React Router 源码深度剖析解决面试中的深层次问题
前端·react.js
光影少年2 小时前
从前端转go开发的学习路线
前端·学习·golang
中微子2 小时前
React Router 面试指南:从基础到实战
前端·react.js·前端框架
3Katrina2 小时前
深入理解 useLayoutEffect:解决 UI "闪烁"问题的利器
前端·javascript·面试
前端_学习之路3 小时前
React--Fiber 架构
前端·react.js·架构
coderlin_3 小时前
BI布局拖拽 (1) 深入react-gird-layout源码
android·javascript·react.js
伍哥的传说4 小时前
React 实现五子棋人机对战小游戏
前端·javascript·react.js·前端框架·node.js·ecmascript·js
qq_424409194 小时前
uniapp的app项目,某个页面长时间无操作,返回首页
前端·vue.js·uni-app
我在北京coding4 小时前
element el-table渲染二维对象数组
前端·javascript·vue.js