v-slot 指令可用 # 缩写具名插槽,仅限静态合法插槽名如 #header;动态插槽名、默认插槽、作用域插槽解构等场景必须用完整 v-slot: 写法。

v-slot 指令简化具名插槽编写,核心是用 # 符号替代 v-slot: 前缀,但仅限于合法、静态的具名插槽场景。
哪些具名插槽能安全缩写为 #
只要子组件中定义了带 name 属性的插槽(如 <slot name="header"></slot>),父组件中就可用 #header 替代 v-slot:header:
-
<template v-slot:header><h1>标题</h1></template>→<template><h1>标题</h1></template> -
<template v-slot:sidebar><nav>菜单</nav></template>→<template><nav>菜单</nav></template> -
<template v-slot:actions><button>保存</button></template>→<template><button>保存</button></template>
注意:# 后直接跟插槽名,不加引号、不加括号、不带空格或特殊字符。例如 #user-info(含短横线)、#1st(数字开头)、#my slot(含空格)都不合法,必须写完整 v-slot:[...] 或改名。
哪些情况不能用 # 缩写
以下情形必须保留完整 v-slot: 写法,# 语法无效:
vue-composition-api-best-practices
Vue 3 组合式 API 与
热门下载
相关下载
精品课程
相关推荐
/
热门推荐
/
最新课程
最新文章
