v-slot 用于父组件向子组件具名插槽传递内容,子组件通过 定义具名插槽;默认插槽用 ,可设默认内容;父组件用 或 #xxx 填充,不可在子组件中使用 v-slot。

子组件中定义具名插槽
在子组件(比如 Card.vue)的 <template></template> 中,用 <slot name="header"></slot> 等方式声明多个可填充区域:
-
<slot></slot>是默认插槽,无需 name 属性 -
<slot name="header"></slot>、<slot name="footer"></slot>就是具名插槽 - 可以给具名插槽加默认内容,比如
<slot name="header">默认标题</slot>,父组件不传时就显示这个
父组件中使用 v-slot 填充具名插槽
在父组件引用子组件时,用 <template v-slot:xxx></template> 或简写 <template></template> 把内容投递到对应名字的插槽:
-
<template v-slot:header><h2>我的标题</h2></template>→ 填入子组件的name="header"插槽 -
<template v-slot:default><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></template>→ 显式填充默认插槽(等价于不写 v-slot 的直接子内容) -
<template><p>© 2026</p> <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></template>→ 简写形式,效果同v-slot:footer
注意几个关键点
不能在子组件模板里写 v-slot —— 那是父组件的语法;
一个子组件标签内可以同时使用多个 <template v-slot:xxx></template>,顺序无关,Vue 会按 name 自动匹配;
如果父组件没提供某个具名插槽的内容,对应位置就显示子组件中该 <slot></slot> 的默认内容(如果有),否则为空。

















