vue3-(插槽的使用)
1.默认插槽的使用:
子组件:
<template> <div class="a"> <div>我是A子组件</div> <slot></slot> </div> </template> <script setup lang="ts"> import { ref } from 'vue'; </script> <style scoped> .a { width: 100%; height: 80px; background: slateblue; text-align: center; font-size: 20px; } </style>
父组件:
<template> <div> <span>我是主体内容部分</span> <button>按钮</button> <A> <template #default> 我是默认插槽内容 </template> </A> </div> </template> <script setup> import A from './A.vue'; </script> <style scoped> div { width: 100%; height: 100%; background: seagreen; text-align: center; font-size: 20px; } </style>
2.具名插槽:给插槽起名,一个子组件可以放多个插槽,根据名字将内容放置在对应的位置
子组件:
<template> <div class="a"> <div>我是A子组件</div> <slot></slot> <slot name="name1"></slot> <slot name="foot"></slot> </div> </template> <script setup lang="ts"> import { ref } from 'vue'; </script> <style scoped> .a { width: 100%; height: 80px; background: slateblue; text-align: center; font-size: 20px; } </style>
父组件:
<template> <div> <span>我是主体内容部分</span> <button>按钮</button> <A> <template #name1>我是name1的插槽内容 </template> <template #foot> 我是foot的插槽内容 </template> <template #default> 我是默认插槽内容 </template> </A> </div> </template> <script setup> import A from './A.vue'; </script> <style scoped> div { width: 100%; height: 100%; background: seagreen; text-align: center; font-size: 20px; } </style>
3.作用域插槽:
子组件:
<template> <div class="a"> <div>我是A子组件</div> <div v-for="item in list" :key="item"> <slot :sad="item"></slot> </div> </div> </template> <script setup lang="ts"> import { ref, reactive } from 'vue'; type names = { name: string; age: number; }; const list = reactive<names[]>([ { name: '张三', age: 18 }, { name: '张三', age: 19 }, { name: '张三', age: 10 }, { name: '张三', age: 13 } ]); </script> <style scoped> .a { width: 100%; height: 80px; background: slateblue; text-align: center; font-size: 20px; } </style>
父组件:
<template> <div> <span>我是主体内容部分</span> <button>按钮</button> <A> <template #default="{ sad }"> <div v-for="(item, index) in sad" :key="index">{{ sad.name }}</div> </template> </A> </div> </template> <script setup> import A from './A.vue'; </script> <style scoped> div { width: 100%; height: 100%; background: seagreen; text-align: center; font-size: 20px; } </style>
4.动态插槽
子组件:
<template> <div class="a"> <div>我是A子组件</div> <slot name="name1"></slot> <slot name="foot"></slot> </div> </template> <script setup lang="ts"> import { ref, reactive } from 'vue'; </script> <style scoped> .a { width: 100%; height: 80px; background: slateblue; text-align: center; font-size: 20px; } </style>
父组件:
<template> <div> <span>我是主体内容部分</span> <button>按钮</button> <A> <template #[foot]> foot的插槽内容</template> <template #[name]> name1的插槽内容</template> </A> </div> </template> <script setup> import A from './A.vue'; import { ref } from 'vue'; const name = ref('name1'); const foot = ref('foot'); </script> <style scoped> div { width: 100%; height: 100%; background: seagreen; text-align: center; font-size: 20px; } </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了