Vue 3 中定义具名插槽最常用简写是用 # 替代 v-slot:,如 #header;默认插槽可省略 template 直接写内容;动态插槽名支持 #[dynamicName];所有插槽必须作为子组件的直接子节点。

Vue 3 中定义具名插槽最常用的简写方式是用 # 符号替代 v-slot:,语法简洁、可读性强,且官方推荐。
基础简写规则
所有 v-slot:xxx 都可直接缩写为 #xxx:
-
v-slot:header→#header -
v-slot:footer→#footer -
v-slot:default→#default(注意:不能省略,#单独写不合法)
默认插槽的特殊简写
默认插槽(即未命名的 <slot></slot>)支持更进一步的简化:
- 完整写法:
<template v-slot:default>...</template> - 简写一:
<template>...</template> - 简写二(最常用):直接把内容写在子组件标签内部,不包
<template></template>—— Vue 会自动识别为默认插槽内容
例如:<mycard><h2>我是标题</h2>
<p>正文内容</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/xiazai/skill13074" title="Vue工具箱(免费版)"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20dee630206.jpg" alt="Vue工具箱(免费版)" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/skill13074" title="Vue工具箱(免费版)">Vue工具箱(免费版)</a>
<p>规避 Vue 3 常见陷阱:响应式、ref 与 reactive、computed 时机与组合式 API 优选实践。</p>
</div>
<a href="/xiazai/skill13074" title="Vue工具箱(免费版)" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div>
<p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p></mycard>,等价于用 #default 包裹。
动态插槽名也支持 # 简写
当插槽名是变量时,依然可用 #[dynamicName] 写法:
- 子组件中:
<slot :name="currentSlot"></slot> - 父组件中:
<template>动态内容</template> - 多个动态插槽可配合
v-for使用,比如<template>...</template>
注意事项
这些写法必须出现在子组件标签的**直接子节点**中,且不能与普通元素混用(除非用 <template></template> 显式包裹):
- ✅ 正确:
<mycard><template>标题</template><template>内容</template></mycard> - ❌ 错误:
<mycard><div><template>标题</template></div></mycard>(<template></template>不再是直接子节点,插槽将不生效)

















