vue2组件插槽的基本使用

文章详细介绍了Vue.js中组件插槽的使用,包括匿名插槽的基本应用,具名插槽在多处内容不确定时的适应性,以及作用域插槽如何让父组件利用子组件数据进行渲染。通过示例展示了如何通过v-slot和slot属性实现内容分发和定制化显示。
摘要由CSDN通过智能技术生成

当A组件要使用多次B组件的结构,但有些B组件的结构需要单独写的时候,使用组件插槽。

匿名插槽的基本使用

// A组件
<B>哈哈哈</B>
<B>嘿嘿嘿</B>

import B from 'B'
components:{B}

//B组件
<template>
<div>
你好
<slot>hello world</slot>
</div>
</template>

匿名插槽只适合在有一处不确定要写什么的时候使用。

上面的案例在A组件里就会显示:

你好 哈哈哈

你好 嘿嘿嘿

B组件定义了slot标签,A组件写在<B>标签里的内容就会渲染到B组件的slot标签处,

同时B组件在slot标签里设置了默认显示内容,如果A组件只写了<B></B>,没往里面写任何东西的话,显示的就是:hello world。

具名插槽

// A组件
<B>
<template v-slot:A1>第一个插槽</template>
<template #A2>第二个插槽</template>
</B>

import B from 'B'
components:{B}

//B组件
<template>
<div>
<slot name='A1'></slot>
<br />
你好
<br />
<slot name='A2'>hello world</slot>
</div>
</template>

具名插槽适合在有多处不确定写什么的时候使用。

上面的案例在A组件里会显示:

第一个插槽

你好

第二个插槽

上面A组件里写的 v-slot:A1 实际上等效于 #A1 , #是v-slot的简写。

作用域插槽

//A组件
<B>
<template v-slot='A'>数据{{A.B1}}</template>
</B>

import B from 'B'
components:{B}

//B组件
<template>
<div>
<slot :B1='b'></slot>
你好
</div>
</template>

data(){
return{b:'hello world'}
}

作用域插槽适合A组件需要用B组件的数据来进行渲染时使用。

上面的案例在A组件里会显示:

数据 hello world 你好

格式为:

A组件:<B><template v-slot='自定义数据名A'>{{自定义数据名A.子组件自定义名B}}</template></B>

B组件:<slot :子组件自定义名B='数据名'></slot>

具名插槽+作用域插槽

//A组件
<B>
<template v-slot:c1='A'>{{A.B1}}</template>
<template #c2='A2'>{{A2.B2}}{{A2.B3}}</template>
</B>

import B from 'B'
components:{B}

//B组件
<template>
<div>
<slot name='c1' :B1='b'></slot>
<br />
<slot name='c2' :B2='b2' :B3='b3'></slot>
</div>
</template>

data(){
return{b:'hello world',b2:'你好 世界',b3:'哈哈哈哈'}
}

上面的案例在A组件里显示为:

hello world

你好 世界哈哈哈哈

格式为:

A组件:<B>

<template v-slot:B组件自定义插槽名1='自定义数据名A'>{{自定义数据名A.子组件自定义名B1}}</template>

<template #B组件自定义插槽名2='自定义数据名A2'>{{自定义数据名A2.子组件自定义名B2}}

{{自定义数据名A2.子组件自定义名B3}}</template>

</B>

B组件:

<slot name='B组件自定义插槽名1' :子组件自定义名B1='数据名1'></slot>

<slot name='B组件自定义插槽名2' :子组件自定义名B2='数据名2' :子组件自定义名B3='数据名3'></slot>

当我们封装一个 Vue 组件时,可以使用插槽(slot)来让组件使用者可以在组件内部插入内容。使用插槽可以让我们的组件更加灵活和可复用。 在组件内部,我们可以使用 `slot` 标签来声明一个插槽。例如: ```html <template> <div class="my-component"> <h2>这是组件的标题</h2> <slot></slot> </div> </template> ``` 上面的代码中,我们在组件的模板中使用了 `slot` 标签,这个标签表示这个组件需要一个插槽使用这个组件的时候,我们可以在组件标签内部插入任意的内容,这些内容会被插入到 `slot` 标签的位置上。 例如,我们可以这样使用这个组件: ```html <template> <div> <MyComponent> <p>这里是插入到组件中的内容</p> </MyComponent> </div> </template> ``` 这样,`<p>这里是插入到组件中的内容</p>` 就会被插入到组件的模板中的 `slot` 标签的位置上。 如果我们需要多个插槽,可以给 `slot` 标签添加一个 `name` 属性来区分不同的插槽。例如: ```html <template> <div class="my-component"> <h2>这是组件的标题</h2> <slot name="header"></slot> <div class="content"> <slot></slot> </div> <slot name="footer"></slot> </div> </template> ``` 上面的代码中,我们声明了三个插槽,分别是名为 `header`、`default` 和 `footer` 的插槽使用这个组件时,我们可以这样插入内容: ```html <template> <div> <MyComponent> <template v-slot:header> <h3>这是头部插槽的内容</h3> </template> <p>这里是默认插槽的内容</p> <template v-slot:footer> <p>这是底部插槽的内容</p> </template> </MyComponent> </div> </template> ``` 上面的代码中,我们使用了带 `v-slot` 的 `<template>` 标签来指定要插入哪个插槽,例如 `v-slot:header` 表示要插入到名为 `header` 的插槽中。在 `<template>` 标签内部,我们可以插入任意的内容,这些内容会被插入到对应的插槽位置上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值