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

vue3中插槽

具名插槽

vue2中会有这样用法:

//父组件
<template>
  <child-component>
    <template slot="empty">
      这是 empty 插槽的内容
    </template>
  </child-component>
</template>

slot=“empty” 这个写法是 Vue 2 中的具名插槽语法,用户可能在某个父组件中通过这个语法向子组件的特定插槽传递内容

而在 Vue 3 中,这种写法被废弃了

在 Vue 3 中,slot=“empty” 的用法已经被替换为新的语法。Vue 3 引入了一种更统一的插槽语法,移除了 Vue 2 中的具名插槽语法 (slot=“name”),改为使用 v-slot 指令。

vue3的用法

<template>
  <child-component>
    <template v-slot:empty >
      这是 empty 插槽的内容
    </template>
  </child-component>
</template>

Vue 3 还支持简写形式,直接使用 #

<template>
  <child-component>
    <template #empty>
      这是 empty 插槽的内容
    </template>
  </child-component>
</template>

总结

  • Vue 2 使用 slot=“empty”
  • Vue 3 使用 v-slot:empty 或简写 #empty

插槽作用域

在Vue3中,​slot-scope​​ 已被废弃,取而代之的是新的 ​​v-slot​​ 语法。虽然 ​​slot-scope​​ 在 Vue2 中用于定义作用域插槽,但在 Vue3 中,推荐使用 ​​v-slot​​ 进行插槽的定义和使用。注意:v-slot只能在template或者vue组件上使用,比如div上使用,会报错

// vue2语法
<div class="custom-tree-node" slot-scope="{ node, data, store }">
</div>

//vue3语法
<template class="custom-tree-node" v-slot="{ node, data, store }">
</template>
//或者针对具名插槽
<template class="custom-tree-node" v-slot:header="{ node, data, store }">
</template>
//也可以使用简写
<template class="custom-tree-node" #header="{ node, data, store }">
</template>

相关文章:

  • ElementUI 级联选择器el-cascader启用选择任意一级选项,选中后关闭下拉框
  • 微服务的认识与拆分
  • 9.1go结构体
  • 第9章 管理日志(网络安全防御实战--蓝军武器库)
  • Linux13-TCP\HTTP
  • 条码扫描手持pda,推动服装零售门店管理效率
  • 大模型开发(四):PET项目——新零售决策评价系统(上)
  • 大白话CSS 优先级计算规则的详细推导与示例
  • HarmonyOS NEXT开发实战:DevEco Studio中DeepSeek的使用
  • uniapp小程序对接腾讯IM即时通讯无ui集成(1)
  • 手机屏幕摔不显示了,如何用其他屏幕临时显示,用来导出资料或者清理手机
  • 77.ObservableCollection使用介绍1 C#例子 WPF例子
  • golang从入门到做牛马:第六篇-Go语言变量存储数据的“小盒子”
  • ubuntu22.04安装RAGFlow配合DeepSeek搭建本地知识库
  • C# ArrayPool
  • LeetCode 102. 二叉树的层序遍历,BFS(广度优先搜索)(Python)
  • CCF-CSP第29次认证第一题 --《田地丈量》
  • C#使用winform实现简单的梯形图指令编译和执行,带编译器和虚拟机代码
  • 浏览器安全问题
  • Go语言集成DeepSeek API和GoFly框架文本编辑器实现流式输出和对话(GoFly快速开发框架)
  • 小白怎么做网页/上海建站seo
  • 孔家庄网站建设/无需下载直接进入的网站的代码
  • 一台ip做两个网站/奶盘seo伪原创工具
  • 做游戏网站思想步骤/网络服务费计入什么科目
  • 17网一起做网店网站/seo培训费用
  • cms做企业网站6/常州网站优化