Vue3之插槽

文章介绍了Vue中插槽的概念和使用方式,包括匿名插槽、具名插槽和作用域插槽。子组件通过<slot>定义占位符,父组件可以填充内容。具名插槽用于多插槽场景,作用域插槽允许传递动态数据给父组件的插槽。示例代码展示了不同类型的插槽用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

插槽:子组件提供一个占位符(<slot></slot>),父组件可以在这个占位符填充HTML、组件等,填充的内容会替换子组件的占位符<slot></slot>

匿名插槽:没有名字,子组件添加占位符,父组件自动匹配

具名插槽:给占位符添加名字,父组件匹配名字后填充内容,适合有多个插槽的时候使用

作用域插槽:在子组件中添加动态数据,给父组件的slot使用

代码案例:

子组件:

<template>

    <div class="main">

        <div class="header">

            <!-- 具名插槽 -->

            <slot name="header"></slot>

        </div>

        <div class="contant">

            <!-- 匿名插槽 -->

            <slot></slot>

        </div>

        <div class="footer">

            <!-- 作用域插槽 -->

            <div v-for="item in 10">

                <slot name="footer" :data="item">

                </slot>

            </div>

        </div>

    </div>

</template>

<script setup lang="ts">

</script>

<style scoped lang="less">

.main {

    width: 100%;

    height: 800px;

    display: flex;

    flex-direction: column;

    div {

        flex: 1;

    }

    .header {

        background: red;

    }

    .contant {

        background: green;

    }

    .footer {

        background: yellow;

    }

}</style>

父组件:

<template>

    <!-- 插槽 -->

    <SlotVue>

        <template v-slot:header>

            <div>我是一个具名插槽</div>

        </template>

        <template v-slot>

            <div>我是一个匿名插槽</div>

        </template>

        <!-- 具名插槽简写和作用域插槽 -->

        <template #footer="{data}">

            <div>作用域插槽</div>

            <span>{{data}}</span>

        </template>

    </SlotVue>

</template>

动态插槽

插槽可以是一个变量名

<SlotVue>

<template #[name]>

        <div>23</div>

</template>

</SlotVue>

const name = ref('header')

### Vue3 Dialog 组件插槽用法 在 Vue3 中,`<dialog>` 组件可以通过定义不同类型的插槽来增强其灵活性和可定制性。以下是关于如何使用 `v-slot` 或者简写形式 `#` 来指定具名插槽以及默认插槽的方式。 #### 默认插槽 当希望向 `<dialog>` 的主体部分传递内容时,可以利用默认插槽: ```html <template> <teleport to="body"> <transition name="fade"> <div v-if="visible" class="modal-overlay" @click.self="closeDialog()"> <!-- 使用默认插槽 --> <slot></slot> </div> </transition> </teleport> </template> <script setup> import { ref } from &#39;vue&#39;; const visible = ref(false); function openDialog() { visible.value = true; } function closeDialog() { visible.value = false; } </script> ``` 在这个例子中,任何放置于父级组件中的标签间的内容都会被渲染到对话框内部作为主体区域的一部分[^1]。 #### 具名插槽 对于更复杂的场景,比如自定义标题栏或操作按钮区,则需要用到具名插槽: ```html <!-- ParentComponent.vue --> <template> <button @click="openDialog">Open Dialog</button> <my-dialog :visible.sync="isVisible"> <!-- 定义名为 "header" 的具名插槽 --> <template #header> <h2>Custom Header Title</h2> </template> <!-- 默认插槽用于主要内容 --> This is the main content of the dialog. <!-- 定义名为 "footer" 的具名插槽 --> <template #footer="{ close }"> <button @click="close()">Cancel</button> <button @click="confirmAction(close)">Confirm</button> </template> </my-dialog> </template> <script setup> import MyDialog from &#39;./MyDialog.vue&#39;; import { ref } from &#39;vue&#39;; let isVisible = ref(false); function openDialog() { isVisible.value = true; } function confirmAction(callback) { alert(&#39;Confirmed!&#39;); callback(); } </script> ``` 上述代码展示了如何通过具名插槽 (`#header`, `#footer`) 向子组件传递特定位置的内容,并且还可以接收来自子组件的数据(如关闭函数),从而实现更加灵活的功能扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值