vue插槽作用域

本文介绍了Vue中子组件如何在slot上定义自定义属性,以及父组件如何通过slot-scope来访问这些属性。示例代码展示了如何在模板中使用slot-scope绑定数据,并通过修改template标签内容获取所需值。在Element-UI框架中,同样可以利用slot实现复杂组件的定制,如在表格中添加编辑和删除操作,并通过scope.row访问数据。
摘要由CSDN通过智能技术生成

子组件在slot上面写自定义属性="数据"

父组件在调用子组件中间写template标签,使用slot-scope="自定义属性"

实例代码:

子组件内容

<template>
  <div>
    <h1>作用域插槽</h1>
    <p v-for="item in list" :key="item.name">
      {{ item.name }}
      今年 {{ item.age }}岁
      <!-- <slot :子自定义属性="数据"></slot> -->
      <slot :row="item"></slot>
    </p>
  </div>
</template>

<script>
export default {
  //用props来接收父组件传递过来的数据
  props: {
    list: {
      type: Array
    }
  }
}
</script>

<style lang="less" scoped>
</style>

父组件内容

<template>
  <div>
    <h1>插槽</h1>
    <slot-scope :list="list">
      <!-- <template slot-scope="父自定义属性"></template> -->
      <template slot-scope="scope"> 性别是{{ scope }} </template>
    </slot-scope>
  </div>
</template>

<script>
import SlotScope from "./components/slot-scope/SlotScope.vue";
export default {
  components: {
    SlotScope,
  },
  data() {
    return {
      list: [
        {
          name: "小鱼",
          age: 12,
          sex: "女",
        },
        {
          name: "小黑",
          age: 13,
          sex: "男",
        },
        {
          name: "小白",
          age: 14,
          sex: "女",
        },
      ],
    };
  },
};
</script>

<style lang="less" scoped>
</style>

打印结果

 更改template标签显示的值就能拿到你想要的值了

<template slot-scope="scope"> 性别是{{ scope.row.sex }} </template>

在Element-ui中也有非常多哦

使用ui框架

<template>
  <el-card class="accont-list">
    <div slot="header">
      <span>账号列表</span>
    </div>
    <div class="content">
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="selection" width="55"> </el-table-column>
        <!-- prop对应列的字段名 -->
        <!-- 账号 -->
        <el-table-column label="账号" prop="account"> </el-table-column>
        <!-- 用户组 -->
        <el-table-column label="用户组" prop="userGroup"> </el-table-column>
        <!-- 创建时间 -->
        <el-table-column label="创建时间" prop="date"> </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[100, 200, 300, 400]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400"
        class="pagination"
      >
      </el-pagination>
      <!-- 操作按钮 -->
      <div class="bottom-box">
        <el-button type="danger" size="small">批量删除</el-button>
        <el-button type="primary" size="small">取消选择</el-button>
      </div>
    </div>
  </el-card>
</template>

<script>
export default {
  name: "AccountList",
  data() {
    return {
      // 列表数据
      tableData: [
        {
          account: "皮卡丘",
          userGroup: "超级管理员",
          date: "2022/5/13",
        },
        {
          account: "皮卡丘",
          userGroup: "超级管理员",
          date: "2022/5/13",
        },
        {
          account: "皮卡丘",
          userGroup: "超级管理员",
          date: "2022/5/13",
        },
      ],
      currentPage4: 1,
    };
  },
  methods: {
    handleSizeChange() {},
    handleCurrentChange() {},
    handleEdit(index, row) {
      console.log(`index`, index);
      console.log(`row`, row);
    },
    handleDelete(index, row) {
      console.log(`index`, index);
      console.log(`row`, row);
    },
  },
};
</script>

<style lang="scss" scoped>
.bottom-box,
.pagination {
  margin-top: 20px;
}
</style>

点击编辑打印结果展示

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苍狼寒刃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值