vue中template中的scope到底是个什么?

在使用ElementUI组件开发项目,我们经常使用template中的作用域插槽,它的作用是在外部获取组件内的数据 ,这里是为了获取这一行的数据,我们让slot-scope值为scope,那么由scope.row就可以得到数据了。拿到这一行的数据,也就可以拿到这一行的id,如图所示:

 <template slot-scope="scope">
    <el-button type="primary" size="small" >分配权限</el-button>
    <el-button type="success" size="small" @click="editHandler(scope.row)">编辑</el-button>
    <el-button type="danger" size="small" @click="delHandler(scope.row)">删除</el-button>
 </template>

那我们怎么验证呢?

错误示范:如果我们直接这么操作,很遗憾是不能在页面上铺出来的,而且可能页面上的数据也会丢失

<template slot-scope="scope">
   {{scope}}
</template>

所以啊,在这里我提供一个小方法,我们定义一个函数,让scope作为参数传递进来,这样我们就在控制台把它打印出来,代码如下:

正确示范:

 <template slot-scope="scope">
    <el-button  @click="fun(scope)">点我看看scope里是什么</el-button>
 </template>

<script>
export default {
    methods:{
        fun(scope){
            console.log(scope)
        }
    }    
}
</script>

快来试试吧😀

  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值