理解vue中的slot-scope与scope.row

文章介绍了Vue2中的作用域插槽特性,如何通过slot-scope在父组件中接收子组件传递的数据。示例展示了在ele-ui的表格组件中,如何利用slot-scope获取并操作当前行的数据,用于自定义操作列,如删除按钮的功能。
摘要由CSDN通过智能技术生成

slot-scope 是vue2中的作用域插槽

在父组件 <template> 上使用特殊的 slot-scope attribute,可以接收子组件传递给插槽的 prop

父组件:

<template>
  <div class="father">
    <h3>这里是父组件</h3>

    <child>
      <template slot-scope="user">
       {{user.data}}
      </template>
    </child>
  </div>
</template>

子组件:

<template>
  <div class="child">

    <h3>这里是子组件</h3>
    // 作用域插槽
    <slot :data="data"></slot>
  </div>
</template>

<script>
 export default {
    data: function() {
      return {
        data: ['john','lisa','amy','mike']
      }
    }
}
</script>

使用vue中的ele-ui组件的时候,我们经常使用template插槽,slot-scope可以获取当前所在元素的数据

<el-table :data="computeData" border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope"> // 此处获取点击的该行的数据
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="del(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
</el-table>

scope.row 中存储着该行的数据:

🔥🔥🔥更多知识,欢迎访问我的个人博客:Nan-ying's Blog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值