Vue3 作用域插槽
在封装组件的过程中,可以为预留的 <slot>
插槽绑定 props
数据,这种带有 props 数据的<slot>
叫做作用域插槽。通过作用域插槽,把组件数据返回给,组件调用方。
MyTest组件
<template>
<div>
<h3>这是 TEST 组件</h3>
<slot :info="infomation" :msg="message"></slot>
</div>
</template>
<script>
export default {
name: 'MyTest',
data() {
return {
// 信息数据
infomation: {
phone: '138xxxx6666',
address: '中国北京',
},
message: 'abc'
}
},
}
</script>
组件调用者
<template>
<div>
<h1>App 根组件</h1>
<hr />
<!-- 使用自定义组件 -->
<my-test>
<!-- 直接解构组件MyTest 返回的数据-->
<template #default="{ msg, info }">
<p>{
{ msg }}</p>
<p>{
{ info.address }}</p>
</template>
</my-test>
<hr />
<my-table>
<template #default="{ user }">
<td>{
{ user.id }}</td>
<td>{
{ user.name }}</td>
<td>
<input type="checkbox" :checked="user.state" />
</td>
</template>
</my-table>
</div>
</template>
<script>
// 导入组件
import MyTest from './MyTest.vue'
export default {
name