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 中。

<!-- 在表达式中使用 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>
<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>
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 的模板代码:

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

ParentComponent 的模板代码:

<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>

相关推荐

  1. slot

    2023-12-10 03:20:02       36 阅读
  2. Vue3 Slot

    2023-12-10 03:20:02       17 阅读
  3. vue slot的使用

    2023-12-10 03:20:02       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-10 03:20:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-10 03:20:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-10 03:20:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-10 03:20:02       18 阅读

热门阅读

  1. 500 行代码 实现 的 Linux 容器 sandbox

    2023-12-10 03:20:02       38 阅读
  2. Spring MVC 接收请求参数所有方式2023-AI

    2023-12-10 03:20:02       32 阅读
  3. LeetCode435. Non-overlapping Intervals

    2023-12-10 03:20:02       27 阅读
  4. dialog打开时重新渲染

    2023-12-10 03:20:02       39 阅读
  5. mysql 语言学习

    2023-12-10 03:20:02       30 阅读
  6. LeetCode-18.四数之和

    2023-12-10 03:20:02       42 阅读
  7. 从 C 到 C++ 编程 — 面向对象编程

    2023-12-10 03:20:02       29 阅读
  8. 【C++容器篇】关联容器知识点总结【超详细】

    2023-12-10 03:20:02       25 阅读
  9. 前端面试提问(4)

    2023-12-10 03:20:02       23 阅读
  10. AlexNet 阅读笔记

    2023-12-10 03:20:02       31 阅读
  11. Qt 鼠标左键推拽界面

    2023-12-10 03:20:02       42 阅读