Vue3中的插槽

本文详细介绍了如何在Vue组件中使用slot功能,包括单slot的使用方法,以及如何在子组件中设置多个slot以便根据名称传递不同内容。通过实例演示了如何在父组件Home中插入文本到Head组件的slot区域,并展示了如何利用模板slot标签来区分不同的slot内容。

在子组件中预留一个slot标签,父组件可以在子组件标签中写入文本或者其他标签,这部分内容会显示在子组件中的slot标签的位置

子组件标签Head

<template>
    head组件<br />
    <slot></slot>
</template>

<script>
    export default{
        name:'Head'
    }
</script>

<style>
</style>

父组件Home

<template>
  <div class="home">
    <Head :title='msg'
        @receive="printMsg"
    >
        Head标签内的文本
    </Head>
  </div>
</template>

<script>
import Head from '@/components/Head.vue'

export default {
  name: 'Home',
  components: {

        Head
  }
}
</script>

多个slot时

子组件中有多个slot时,以name属性区分

Head组件

 

<template>
    head组件<br />
    <slot></slot>

   <slot name="s1"></slot>
    <slot name="s2"></slot>
</template>

<script>
    export default{
        name:'Head'
    }
</script>

<style>
</style>

Home组件

 

<template>
  <div class="home">
    <Head :title='msg'
        @receive="printMsg"
    >
        Head标签内的文本

        <template v-slot:s1>
            <label>slot1的内容</label>
        </template>
        <template v-slot:s2>
            <label>slot2的内容</label>
        </template>
    </Head>
  </div>
</template>

<script>
import Head from '@/components/Head.vue'

export default {
  name: 'Home',
  components: {

        Head
  }
}
</script>

Vue 3中,插槽的使用方式与Vue 2有所不同。Vue 3中引入了新的组件声明方式,即使用`<script setup>`语法来定义组件。下面是Vue 3插槽的使用方法: 1. 默认插槽: 在父组件中,可以使用`<slot>`标签来定义默认插槽。子组件中的内容将会被插入到父组件中的`<slot>`标签所在的位置。例如: ```vue <!-- ParentComponent.vue --> <template> <div> <slot></slot> </div> </template> <!-- ChildComponent.vue --> <template> <div> <h1>Child Component</h1> <p>This is the content of the child component.</p> </div> </template> ``` 在父组件中使用子组件时,可以将子组件的内容放在父组件的标签内: ```vue <!-- App.vue --> <template> <div> <parent-component> <h2>Parent Component</h2> <p>This is the content of the parent component.</p> </parent-component> </div> </template> ``` 运行后,父组件中的`<slot>`标签将会被子组件的内容替换。 2. 具名插槽: 在父组件中,可以使用`<slot>`标签的`name`属性来定义具名插槽。子组件中的内容可以通过`<template v-slot:插槽名>`或`<template #插槽名>`来指定插入到具名插槽中。例如: ```vue <!-- ParentComponent.vue --> <template> <div> <slot name="header"></slot> <slot></slot> </div> </template> <!-- ChildComponent.vue --> <template> <div> <template v-slot:header> <h1>Header</h1> </template> <h2>Child Component</h2> <p>This is the content of the child component.</p> </div> </template> ``` 在父组件中使用子组件时,可以使用`<template v-slot:插槽名>`或`<template #插槽名>`来指定具名插槽的内容: ```vue <!-- App.vue --> <template> <div> <parent-component> <template v-slot:header> <h2>Parent Component Header</h2> </template> <h3>Parent Component</h3> <p>This is the content of the parent component.</p> </parent-component> </div> </template> ``` 运行后,父组件中的`<slot name="header">`标签将会被子组件的具名插槽内容替换。 3. 作用域插槽: 在Vue 3中,作用域插槽被称为"带有参数的插槽"。父组件可以通过在`<slot>`标签上使用`v-slot:参数名`或`#参数名`来接收子组件传递的数据。例如: ```vue <!-- ParentComponent.vue --> <template> <div> <slot name="header" v-bind:user="user"></slot> </div> </template> <!-- ChildComponent.vue --> <template> <div> <template v-slot:header="slotProps"> <h1>{{ slotProps.user.name }}</h1> </template> <h2>Child Component</h2> <p>This is the content of the child component.</p> </div> </template> <script> export default { data() { return { user: { name: &#39;John&#39;, age: 25 } }; } }; </script> ``` 在父组件中使用子组件时,可以通过`v-slot:参数名`或`#参数名`来接收子组件传递的数据: ```vue <!-- App.vue --> <template> <div> <parent-component> <template v-slot:header="slotProps"> <h2>{{ slotProps.user.name }} - {{ slotProps.user.age }}</h2> </template> <h3>Parent Component</h3> <p>This is the content of the parent component.</p> </parent-component> </div> </template> ``` 运行后,父组件中的`<slot name="header" v-bind:user="user">`标签将会被子组件的作用域插槽内容替换,并且可以访问子组件传递的数据。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值