当前位置: 首页 > ai >正文

Vue2 与 Vue3 的插槽(默认插槽、具名插槽、作用域插槽、具名插槽 + 作用域插槽)

Vue2 插槽

1、默认插槽
<!-- 子组件 --><template><div><slot>默认内容</slot></div>
</template>
<!-- 父组件,使用子组件 --><child><div>新的内容<div>
</child>
2、具名插槽
  1. 使用 slot="【name】" 的具名插槽(已废弃)
<!-- 子组件 --><template><div><slot name="header"></slot><slot></slot><slot name="footer"></slot></div>
</template>
<!-- 父组件,使用子组件 --><child><template slot="header"><h1>标题</h1></template><div>默认内容</div><template slot="footer"><p>页脚</p></template>
</child>
  1. 使用 v-slot:【name】 的具名插槽(推荐使用)
<!-- 子组件 --><template><div><slot name="header"></slot><slot></slot><slot name="footer"></slot></div>
</template>
<!-- 父组件,使用子组件 --><child><template v-slot:header><h1>标题</h1></template><div>默认内容</div><template v-slot:footer><p>页脚</p></template>
</child>
3、作用域插槽
  1. 使用 scope="【data】" 的作用域插槽(已废弃)
<!-- 子组件 --><template><ul><li v-for="item in items" :key="item.id"><slot :item="item"></slot></li></ul>
</template>
<!-- 父组件,使用子组件 --><child><template scope="props"><span>{{ props.item.name }}</span></template>
</child>
<!-- 解构赋值 --><child><template scope="{ item }"><span>{{ item.name }}</span></template>
</child>
  1. 使用 slot-scope:"【data】" 的作用域插槽(推荐使用)
<!-- 子组件 --><template><ul><li v-for="item in items" :key="item.id"><slot :item="item"></slot></li></ul>
</template>
<!-- 父组件,使用子组件 --><child><template slot-scope="props"><span>{{ props.item.name }}</span></template>
</child>
<!-- 解构赋值 --><child><template slot-scope="{ item }"><span>{{ item.name }}</span></template>
</child>
4、具名插槽 + 作用域插槽
  • 使用 v-slot:【name】="【data】"
<!-- 子组件 --><template><ul><li v-for="item in items" :key="item.id"><slot name="header" :item="item"></slot><slot name="footer" :item="item"></slot></li></ul>
</template>
<!-- 父组件,使用子组件 --><child><template v-slot:header="props"><h3>{{ props.item.name }}(头部)</h3></template><template v-slot:footer="props"><p>{{ props.item.description }}(底部)</p></template>
</child>
<!-- 解构赋值 --><child><template v-slot:header="{ item }"><h3>{{ item.name }}(头部)</h3></template><template v-slot:footer="{ item }"><p>{{ item.description }}(底部)</p></template>
</child>

Vue3 插槽

1、默认插槽
<!-- 子组件 --><template><div><div>this is child</div><slot></slot></div>
</template>
<!-- 父组件,使用子组件 --><Child><div>123</div>
</Child>
2、具名插槽
  1. 使用 v-slot:【name】 的具名插槽
<!-- 子组件 --><template><div><div>this is child</div><header><slot name="header"></slot></header><main><slot></slot></main><footer><slot name="footer"></slot></footer></div>
</template>
<!-- 父组件,使用子组件 --><Child><template v-slot:header><h1>页面标题</h1></template><div>主要内容区域</div><template v-slot:footer><p>版权信息</p></template>
</Child>
  1. 使用 #【name】 的具名插槽(简写)
<!-- 子组件 --><template><div><div>this is child</div><header><slot name="header"></slot></header><main><slot></slot></main><footer><slot name="footer"></slot></footer></div>
</template>
<Child><template #header><h1>页面标题</h1></template><div>主要内容区域</div><template #footer><p>版权信息</p></template>
</Child>
3、作用域插槽
  1. 使用 v-slot="【data】" 的作用域插槽
<!-- 子组件 --><template><div><div>this is child</div><slot :item="item"></slot></div>
</template>
<!-- 父组件,使用子组件 --><Child><template v-slot="props"><span>{{ props.item.name }}</span></template>
</Child>
<!-- 解构赋值 --><Child><template v-slot="{ item }"><span>{{ item.name }}</span></template>
</Child>
  1. 使用 #="【data】" 的作用域插槽(简写)
<!-- 子组件 --><template><div><div>this is child</div><slot :item="item"></slot></div>
</template>
<!-- 父组件,使用子组件 --><Child><template #="props"><span>{{ props.item.name }}</span></template>
</Child>
<!-- 解构赋值 --><Child><template #="{ item }"><span>{{ item.name }}</span></template>
</Child>
4、具名插槽 + 作用域插槽
  1. 使用 v-slot:【name】="【data】"
<!-- 子组件 --><template><div><div>this is child</div><header><slot name="header" :item="item"></slot></header><footer><slot name="footer" :item="item"></slot></footer></div>
</template>
<!-- 父组件,使用子组件 --><Child><template v-slot:header="props"><span>{{ props.item.name }}(头部)</span></template><template v-slot:footer="props"><span>{{ props.item.name }}(底部)</span></template>
</Child>
<!-- 解构赋值 --><Child><template v-slot:header="{ item }"><span>{{ item.name }}(头部)</span></template><template v-slot:footer="{ item }"><span>{{ item.name }}(底部)</span></template>
</Child>
  1. 使用 #【name】="【data】"
<!-- 子组件 --><template><div><div>this is child</div><header><slot name="header" :item="item"></slot></header><footer><slot name="footer" :item="item"></slot></footer></div>
</template>
<!-- 父组件,使用子组件 --><Child><template #header="props"><span>{{ props.item.name }}(头部)</span></template><template #footer="props"><span>{{ props.item.name }}(底部)</span></template>
</Child>
<!-- 解构赋值 --><Child><template #header="{ item }"><span>{{ item.name }}(头部)</span></template><template #footer="{ item }"><span>{{ item.name }}(底部)</span></template>
</Child>
http://www.xdnf.cn/news/13548.html

相关文章:

  • lesson05-手写数据问题案例实战(理论+代码)
  • linux回收站
  • 爱普生TG5032SGN同步以太网的高精度时钟解决方案
  • P2840 纸币问题 2
  • 华为OD机考-数字螺旋矩阵(JAVA 2025B卷)
  • Python前端系列(三)
  • DATABASE 结构迁移实战手册:脚本生成、分类与部署全流程详解
  • 华为云Flexus+DeepSeek征文|华为云CCE容器高可用部署Dify LLM应用后的资源释放指南
  • 掌握Linux进程替换:从原理到实战(自定义shell)
  • 笔试模拟day1
  • 随记 使用certbot申请ssl证书
  • 跨域的本质与实战:从理论到松鼠短视频系统的演进-优雅草卓伊凡|卢健bigniu
  • 数据库游标:逐行处理数据的“手术刀”——从原理到实战的深度解析
  • 开关电源-KA3842A芯片的电路分析
  • CSS“多列布局”
  • 电池充放电容量检测:能否精准锁定电池真实性能?
  • PSCAD closed loop buck converter
  • 打卡day51
  • CMake安装教程
  • 2025GEO供应商排名深度解析:源易信息构建AI生态优势
  • 新德通:光通信领域的硬核力量,引领高速互联新时代
  • Appium + Node.js 测试全流程
  • 最接近的三数之和
  • Java 基础知识填空题(共 10 题)
  • 6.ref创建对象类型的响应式数据
  • FPGA实现VESA DSC编码功能
  • 【游戏项目】大型项目Git分支策略与开发流程设计构想
  • 无人机智能运行系统技术解析
  • 为进行性核上性麻痹患者定制:饮食健康指南
  • 全球首个体重管理AI大模型“减单”发布,学AI大模型来近屿智能