vue二次封装组件

3个待解决问题

  1. 属性传递
  2. 事件传递
  3. 插槽传递

前提知识点

以封装Ant Design Vue Table为例

二次封装组件

二次封装组件名为GxTable,简单的覆盖bordered和pagination属性,封装序号列的模板。

<template>
  <a-table
    v-bind="$props"
    v-on="$listeners"
    :rowKey="(record, index) => index"
  >
    <template #index="{ record, index }">
      <a-checkbox v-model:checked="record.checked"></a-checkbox>
      &nbsp;{{ index + 1 }}
    </template>

    <template v-for="(value, key) in $slots" #[key]="{ text, record, index }">
      <slot :name="key" :text="text" :record="record" :index="index"></slot>
    </template>
  </a-table>
</template>

<script>
import { Table } from 'ant-design-vue';
export default {
  name: 'GxTable',
  props: {
    ...Table.props,
    bordered: {
      type: Boolean,
      default: true,
    },
    pagination: {
      type: Boolean,
      default: false,
    },
  },
};
</script>

新组件使用

新组件使用方式和原组件一模一样,在原组件的基础上,新组件扩展的属性或方法或插槽都和普通组件无区别。

<template>
	<GxTable
	  :columns="columns"
	  :dataSource="dataSource"
	>
	  <template #age="{ text }">
	    <a-button type="primary">{{text}}</a-button>
      </template>
    </GxTable>
</template>

<script>
export default {
  name: 'UseGxTableDemo',
  data() {
    return {
      columns: [
        { title: '序号', dataIndex: 'index', width: 80, fixed: 'left', slots: { customRender: 'index' } },
        { title: '姓名', dataIndex: 'name', slots: { customRender: 'name' } },
        { title: '年龄', dataIndex: 'age', slots: { customRender: 'age' } },
      ],
      dataSource: [
        { key: '1', name: '张三疯', age: 32 },
      ],
    };
  },
};
</script>

效果图

效果图

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高献涛

如意如意,随你心意,快快显灵

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

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

打赏作者

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

抵扣说明:

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

余额充值