slot-scope用法及理解

下面是2个父子的vue组件,先解释一下2个组件做了什么事情

  • 父组件仅仅是调用了子组件
  • 子组件内部实现了一个todolist列表

我建议从数据流动的角度,理解插槽作用域的使用方式,(先学会怎么用,暂时不用理解为什么要这么用,使用场景是第二部分)

  • 1.父组件传递了todos数组给子组件
  • 2.子组件通过props接受了数组数据,这里应该没有任何问题
  • 3.子组件拿到数组后v-for渲染列表,并且通过 <slot :todo="todo">的方式,把数组内的每一个todo对象,传递给父组件
  • 4.父组件通过slot-scope="slotProps"的方式,接受todo对象,之后就可以通过slotProps.todo.xxx的方式来使用了

所以数据的流动经历了

  • 父组件传递todos数组给子组件
  • 子组件遍历todos数组,把里面的todo对象传递给父组件

好啦,这就是slot-scope的使用方法,就这么简单,完结撒花~

我贴出全部代码,方便大家自己研究

父组件的源码,也就是调用者

<template>
  <todo-list :todos="todos">
    <template slot-scope="slotProps">
      <span v-if="slotProps.todo.isComplete">✓</span>
      <span>{{slotProps.todo.text}}</span>
    </template>
  </todo-list>
</template>

<script>
import todoList from './todoList'
export default {
  data () {
    return {
      todos: [
        {
          id: 0,
          text: 'ziwei0',
          isComplete: false
        },
        {
          text: 'ziwei1',
          id: 1,
          isComplete: true
        },
        {
          text: 'ziwei2',
          id: 2,
          isComplete: false
        },
        {
          text: 'ziwei3',
          id: 3,
          isComplete: false
        }
      ]
    }
  },

  components: {
    todoList
  },

}
</script>

子组件源码,也就是封装组件的人

<template>
  <ul>
    <li v-for="todo in todos" :key="todo.id">
      <slot :todo="todo">
      </slot>
    </li>
  </ul>
</template>

<script>
export default {
  props: {
    todos: {
      type: Array
    }
  }
}
</script>

理解关键点:

slot-scope="slotProps"

这个slotProps是自定义的一个名字,这个名字可以接收到来自子组件的值。

本篇文章节选转自https://segmentfault.com/a/1190000015884505 

  • 29
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要使用 `element-plus` 的 `slot-scope` 属性来自定义表头,你可以在 `<el-table-column>` 标签中使用 `slot` 属性来定义自己的模板,并在模板中使用 `slot-scope` 来获取列的相关信息。 下面是一个示例,假设你想在表头中添加一个按钮来触发排序操作: ```html <template> <el-table :data="tableData"> <el-table-column prop="name" label="Name"> <template slot="header" slot-scope="{ column }"> <div> {{ column.label }} <el-button type="text" icon="el-icon-arrow-down" @click="sortTable(column.prop, 'desc')">降序</el-button> <el-button type="text" icon="el-icon-arrow-up" @click="sortTable(column.prop, 'asc')">升序</el-button> </div> </template> </el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> </template> ``` 在上面的示例中,我们在 `name` 列的表头中添加了两个按钮来触发排序操作。我们使用 `slot="header"` 来定义自己的表头模板,并使用 `slot-scope="{ column }"` 来获取 `column` 对象,该对象包含了当前列的一些信息,包括 `prop` 和 `label` 等。 然后我们就可以在模板中使用 `column` 对象来获取当前列的 `label`,并将两个排序按钮与其一起渲染出来。 最后,我们还可以在按钮的 `click` 事件中调用 `sortTable` 方法来触发排序操作。在这里,我们将 `column.prop` 和排序方式作为参数传递给 `sortTable` 方法,以便在方法中根据这些参数进行排序。 希望这个示例能够帮助你理解如何使用 `element-plus` 的 `slot-scope` 属性来自定义表头。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值