插槽(默认插槽、具名插槽、作用域插槽、具名作用域插槽)
1.默认插槽
父组件
<script setup> import Gategory from './components/Category.vue' import {ref} from 'vue' let foods=ref(['火锅','烧烤','小龙虾']) let games=ref(['LOL','王者荣耀','qq飞车']) let films=ref(['教父','拆弹专家','碟中谍']) </script> <template> <div class="container"> <Gategory title="食物" > dddd </Gategory> <Gategory title="游戏" > <ul v-for="(g,index) in games" :key="index"> <li>{{g}}</li> </ul> </Gategory> <Gategory title="游戏" > <a href="http://www.baidu.com">http://www.baidu.com</a> </Gategory> </div> </template> <style lang="css"> .container{ display:flex; justify-content: space-around; } </style>
子组件
<script setup> import {defineProps, toRefs} from 'vue'; const props=defineProps({ todoList:Array, title:String }) let {todoList,title}=toRefs(props) </script> <template> <div class="category"> <h3>{{title}}分类</h3> <!-- <ul v-for="(l,index) in todoList" :key="index"> <li>{{l}}</li> </ul> --> <slot></slot> </div> </template> <style> .category{ background-color: aqua; width: 100px; height: 200px; } h3{ background-color: red; text-align: center; } </style>
2.具名插槽:给插槽带上标识,按名匹配插槽
父组件
<Gategory title="食物" > <template v-slot:header> <div>dddd</div> </template><br> <template v-slot:footer> <a href="http://www.baidu.com">跳转百度</a> </template> </Gategory>
子组件
<template> <div class="category"> <h3>{{title}}分类</h3> <slot name="footer"></slot> <slot name="header"></slot> </div> </template>
3.作用域插槽:子父组件利用slot传输数据
父组件
<template> <div class="container"> <Gategory > <template v-slot="games" > <ul v-for="(g,index) in games.games" :key="index"> <li>{{g}}</li> </ul> </template> </Gategory> <Gategory > <template v-slot="games"> <ol v-for="(g,index) in games.games" :key="index"> <li>{{g}}</li> </ol> </template> </Gategory> <Gategory v-slot="games" > <h4 v-for="(g,index) in games.games" :key="index"> {{g}} </h4> </Gategory> </div> </template>
子组件
<script setup> import {ref} from 'vue' let games=ref(['LOL','王者荣耀','qq飞车']) </script> <template> <div class="category"> <slot :games="games"></slot> </div> </template> <style>
4.具名作用域插槽
父组件
<template> <div class="container"> <Gategory > <template v-slot:header="{games,news}"> <h2> {{news}} </h2> <ul v-for="(g,index) in games" :key="index"> <li>{{g}}</li> </ul> </template> </Gategory> <Gategory > <template v-slot:center="slotProps"> <ol v-for="(g,index) in slotProps.games" :key="index"> <li>{{g}}</li> </ol> </template> </Gategory> <Gategory> <template v-slot:footer="{foods}" > <h4 v-for="(g,index) in foods" :key="index"> {{g}} </h4> </template> </Gategory> </div> </template>
子组件
<script setup> import {ref} from 'vue' let games=ref(['LOL','王者荣耀','qq飞车']) let love=ref('love') let foods=ref(['烧烤','火锅']) </script> <template> <div class="category"> <slot name="header" :games="games" :news="love"></slot> <slot name="center" :games="games" :news="love"></slot> <slot name="footer" :foods="foods"></slot> </div> </template>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix