前端工作总结228-关于Vue中的slot-scope=“scope“

<template>
  <el-table :data="tableData" style="width: 100%">
    //---:data="用于存放请求数据回来的数组"
    <el-table-column label="索引值" width="400">
      <template slot-scope="scope">
        //--- 这里取到当前单元格
        <span>{{ scope.$index }}</span>//--- scope.$index就是索引
      </template>
    </el-table-column>
    <el-table-column label="标题" width="350">
      <template slot-scope="scope">
        //--- 这里取到当前单元格
        <span>{{ scope.row.title }}</span>
        //--- scope.row 直接取到该单元格对象,就是数组里的元素对象,即是tableData[scope.$index]
        //---.title 是对象里面的title属性的值
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        //--- 这里取到当前单元格
        <el-dropdown size="medium" split-button type="primary">
          更多
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native.prevent="handleEdit(scope.$index, scope.row)">
              编辑
            </el-dropdown-item>
            <el-dropdown-item @click.native.prevent="getUp(scope.$index, scope.row)">
              上升
            </el-dropdown-item>
            <el-dropdown-item @click.native.prevent="getDown(scope.$index, scope.row)">
              下降
            </el-dropdown-item>
            <el-dropdown-item @click.native.prevent="handleDelete(scope.$index, scope.row)">
              删除
            </el-dropdown-item>
            //---这里的点击事件已经不是在根元素上了,因为多套了几层结构。
            //---这里的点击事件如果没有加上 .native 则点击无效!
            //---这里的点击事件要加上 .native 表示监听组件根元素的原生事件。
            //---这里的点击事件不需要 .prevent 也可以实现相同效果
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </el-table>
</template>
 
<script>
export default {
  data () {
    return {
      tableData: [{ title: 123, age: 11 }, { title: 456, age: 18 }]
      // ---为了效果先给值,一般情况下为空,其实际值是后台接口请求回来的
    }
  },
  methods: {
    handleDelete (index, row) {
      this.tableData.splice(index + 1, 1)// ---前端删除index要+1 !!!!!!!
      // ---下面是后端数据删除,可以不看
      axios.post(config.newsDelete, // ---后端数据删除
        {
          id: row.id// ---传入被删除的对象的id值
        },
        {
          headers: {
            Authorization: 'Bearer ' + sessionStorage.getItem('token')// ---请求头验证
          }
        }
      )
        .then((res) => {
          this.rendering()// ---删除了重新渲染
        })
    }
  }
}
</script>
 
<style>
 
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 2.6.0+ 版本,`slot-scope` 被废弃了,用 `v-slot` 代替。`v-slot` 是 Vue.js 2.6.0+ 新增的语法糖,用于替代之前版本的 `slot-scope`。 `v-slot` 用于在组件定义插槽,并且可以将插槽内部的数据传递给子组件。使用 `v-slot` 可以让组件的语法更加简洁易懂,同时也可以提高组件的可维护性和可复用性。 在使用 `v-slot` 的时候,需要在组件内部使用 `<slot>` 标签定义插槽,并且给定一个名字。在父组件使用 `v-slot` 指令定义与子组件同名的插槽,并且将插槽内部的数据绑定到父组件的数据。 例如,以下代码使用了 `v-slot` 替代了 `slot-scope`: ``` <template> <div> <child-component> <template v-slot:default="slotProps"> <span>{{ slotProps.text }}</span> </template> </child-component> </div> </template> ``` 在上面的代码,`<child-component>` 是一个子组件,它定义了一个名为 `default` 的插槽。在父组件,使用 `v-slot` 指令来定义与子组件同名的插槽,并且将插槽内部的数据 `slotProps.text` 绑定到了父组件的数据。 需要注意的是,在使用 `v-slot` 的时候,如果只是简单的传递数据,可以使用 `v-slot:default` 的简写形式 `#default`,如下所示: ``` <template> <div> <child-component> <span #default="slotProps">{{ slotProps.text }}</span> </child-component> </div> </template> ``` 这样可以让代码更加简洁易懂。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值