您的位置:首页 > 娱乐 > 明星 > vue3插槽slot的使用

vue3插槽slot的使用

2024/10/6 0:26:07 来源:https://blog.csdn.net/weixin_46243043/article/details/139845731  浏览:    关键词:vue3插槽slot的使用

一,默认插槽

父组件页面:使用子组件标签

<template><div>我是父组件自己的内容</div><ComTest></ComTest>  // 这里使用子组件的内容<!-- <ComTest>我要替换默认插槽的内容</ComTest>  // 这里替换子组件的内容  -->
</template>
<script setup lang='ts'>import ComTest from '../components/ComTest.vue';
</script>

子组件:使用<slot></slot>

<template><div ><slot>我是默认插槽</slot></div>
</template>

二,具名插槽

父组件页面:使用 <template v-slot:插槽名></template >

<template><div>我是父组件自己的内容</div><ComTest ><!--插槽语法糖 v-slot:header 等于 #header  --><template v-slot:header>   替换具名插槽头部默认信息</template><!--插槽语法糖 v-slot:main 等于 #main  --><template v-slot:main>替换具名插槽中间默认信息</template><!--插槽语法糖 v-slot:footer 等于 #footer--><template v-slot:footer>替换具名插槽底部默认信息</template></ComTest>
</template>

子组件:使用<slot name="插槽名"></slot>

<template><div ><header><slot name="header">我是具名插槽头部默认信息</slot></header><main><slot name="main">我是具名插槽中间默认信息</slot></main><footer><slot name="footer">我是具名插槽底部默认信息</slot></footer></div>
</template>

三,作用域插槽

父组件页面:使用 <template v-slot:插槽名 = "scope">{{scope.属性名}}</template >

<template><div>我是父组件自己的内容</div><ComTest ><template v-slot:header="scope">{{ scope.msg}}  <!-- 我是作用域插槽头部信息 --></template><template v-slot:main="scope">{{ scope.msg  }}<!-- 我是作用域插槽中间信息 --></template><template v-slot:footer="scope">{{ scope.msg  }}<!-- 我是作用域插槽底部信息 --></template></ComTest>
</template>

子组件:使用<slot name="插槽名" 属性名='属性值'></slot>

<template><div ><header><slot name="header" msg="我是作用域插槽头部信息">我是具名插槽头部默认信息</slot></header><main><slot name="main" msg="我是作用域插槽中间信息">我是具名插槽中间默认信息</slot></main><footer><slot name="footer" msg="我是作用域插槽底部信息">我是具名插槽底部默认信息</slot></footer></div>
</template>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com