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>
相关推荐
eason_fan1 小时前
从一则内存快照看iframe泄漏:活跃与Detached状态的回收差异
前端·性能优化
狗头大军之江苏分军1 小时前
年底科技大考:2025 中国前端工程师的 AI 辅助工具实战盘点
java·前端·后端
编程修仙2 小时前
第三篇 Vue路由
前端·javascript·vue.js
比老马还六2 小时前
Bipes项目二次开发/硬件编程-设备连接(七)
前端·javascript
掘金一周2 小时前
前端一行代码生成数千页PDF,dompdf.js新增分页功能| 掘金一周 12.25
前端·javascript·后端
张就是我1065922 小时前
漏洞复现指南:利用 phpinfo() 绕过 HttpOnly Cookie 保护
前端
Kagol2 小时前
🎉TinyVue v3.27.0 正式发布:增加 Space 新组件,ColorPicker 组件支持线性渐变
前端·vue.js·typescript
潍坊老登2 小时前
大前端框架汇总/产品交互参考UE
前端
方安乐3 小时前
获取URL参数如何避免XSS攻击
前端·xss
十二AI编程3 小时前
MiniMax M2.1 实测,多语言编程能力表现出色!
前端