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>
相关推荐
摇摇奶昔x14 分钟前
webpack 学习
前端·学习·webpack
阿珊和她的猫33 分钟前
Vue Router中的路由嵌套:主子路由
前端·javascript·vue.js
_龙小鱼_41 分钟前
Kotlin 作用域函数(let、run、with、apply、also)对比
java·前端·kotlin
霸王蟹1 小时前
React 19中如何向Vue那样自定义状态和方法暴露给父组件。
前端·javascript·学习·react.js·typescript
小野猫子1 小时前
Web GIS可视化地图框架Leaflet、OpenLayers、Mapbox、Cesium、ArcGis for JavaScript
前端·webgl·可视化3d地图
shenyan~1 小时前
关于 js:9. Node.js 后端相关
前端·javascript·node.js
uwvwko1 小时前
ctfshow——web入门254~258
android·前端·web·ctf·反序列化
所待.3832 小时前
深入解析SpringMVC:从入门到精通
前端·spring·mvc
逃逸线LOF2 小时前
CSS之精灵图(雪碧图)Sprites、字体图标
前端·css
进取星辰3 小时前
31、魔法生物图鉴——React 19 Web Workers
开发语言·javascript·ecmascript