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>
相关推荐
百万蹄蹄向前冲2 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙2 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan3 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen4 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理4 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
IT_陈寒5 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端
Blanche15005 小时前
利用 RAG 为答疑机器人扩展知识范围
前端
天若有情6735 小时前
【纯前端小工具】公历生日转农历,批量查询每年农历生日对应的公历日期(GitHub Pages在线直接用)
前端·javascript·github pages·农历转换·lunisolar·网页小工具
SoonITer5 小时前
怎样构建一个 Agent-friendly 的网站
前端·agent
颜进强6 小时前
01 · NestJS 是什么:用途、解决什么问题、与热门框架对比
前端·后端·ai编程