slot插槽

用来访问被插槽分发的内容。每个具名插槽有其相应的 property (例如:v-slot:foo 中的内容将会在 vm.$slots.foo 中被找到)。default property 包括了所有没有被包含在具名插槽中的节点,或 v-slot:default 的内容。
请注意插槽不是响应性的。如果你需要一个组件可以在被传入的数据发生变化时重渲染,我们建议改变策略,依赖诸如 propsdata 等响应性实例选项。
注意:v-slot:foo2.6 以上的版本才支持。对于之前的版本,你可以使用废弃了的语法。

在使用渲染函数书写一个组件时,访问 vm.$slots 最有帮助。
v-slot 指令自 Vue 2.6.0 起被引入,提供更好的支持 slot 和 slot-scope attribute 的 API 替代方案。v-slot 完整的由来参见这份 RFC。在接下来所有的 2.x 版本中 slotslot-scope attribute 仍会被支持,但已经被官方废弃且不会出现在 Vue 3 中。

html 复制代码
<!-- 在表达式中使用 ES2015 解构 -->
<todo-list v-bind:todos="todos">
  <template slot="operation" slot-scope="{ todo }"> 
    <span v-if="todo.isComplete">✓</span>
    {{ todo.text }}
  </template>
   <template slot="default" slot-scope="slotProps">
    {{ slotProps.msg }}
  </template>
</todo-list>
html 复制代码
<todo-list v-bind:todos="todos">
  <template v-slot:operation="{ todu }" > 
    <span v-if="todo.isComplete">✓</span>
    {{ todo.text }}
  </template>
   <template v-slot:default="slotProps">
    {{ slotProps.msg }}
  </template>
</todo-list>
javascript 复制代码
Vue.component('blog-post', {
  render: function (createElement) {
    var header = this.$slots.header
    var body   = this.$slots.default
    var footer = this.$slots.footer
    return createElement('div', [
      createElement('header', header),
      createElement('main', body),
      createElement('footer', footer)
    ])
  }
})

v-slot:other="otherSlotProps" 这样的语法来将数据传递给插槽

ChildComponent 的模板代码:

html 复制代码
<template>
  <div>
    <slot name="other" :otherSlotProps="otherSlotProps"></slot>
  </div>
</template>
<script>
export default {
  data() {
    return {
      otherSlotProps: {
        message: 'Hello from parent component'
      }
    };
  }
};
</script>

ParentComponent 的模板代码:

html 复制代码
<template>
  <div>
    <ChildComponent>
      <template v-slot:other="otherSlotProps">
        <p>{{ otherSlotProps.message }}</p>
      </template>
    </ChildComponent>
  </div>
</template>
<script>
	import ChildComponent from './ChildComponent.vue';
	export default {
	  components: {
	    ChildComponent
	  }
	};
</script>
相关推荐
kyriewen8 小时前
微软用Go重写TypeScript编译器,速度提升10倍,网友:这是“背叛”还是“救赎”?
前端·typescript·ecmascript 6
Ceelog8 小时前
久坐党自救指南:屏幕前 8 小时,身体到底在经历什么
前端·后端
西陵9 小时前
Agent 为什么会陷入 Doom Loop?OpenClaw 的破解之道
前端·人工智能·ai编程
Hyyy9 小时前
普通前端续命周报——第2周
前端
wuxinyan1239 小时前
工业级大模型学习之路030:Streamlit 企业级智能体前端工作台
前端·学习·streamlit·智能体
修己xj9 小时前
告别无效刷屏!TrendRadar:最快30秒部署的开源热点助手,让你只看真正关心的新闻
前端
anOnion10 小时前
构建无障碍组件之Slider Pattern
前端·html·交互设计
云水一下11 小时前
JavaScript 从零基础到精通系列:前世今生与编程启蒙
前端·javascript
月亮邮递员61611 小时前
Markdown语法总结
开发语言·前端·javascript
Kurisu57511 小时前
雾锁王国修改器下载2026最新
前端·修改器代码