vue3 使用element-plus 如何再次封装table组件

news/2024/4/27 19:34:50

• vue3 使用element-plus 如何再次封装table组件

• 基本步骤

• 创建子组件:

• 默认数据配置

• 在需要使用自定义 Table 组件的地方引入并使用:

创建子组件:

创建一个新的 .vue 文件,例如子组件 baseTable.vue,作为你封装后的 Table 组件。

 <template><div class="base-table-wrapper"><el-table :data="tableData" style="width: 100%"><template v-for="item in column" :key="item.prop"><el-table-column :prop="item.prop" :label="item.label" :width="item.width"><template #default="scope" v-if="!!item.isScope"><slot :name="item.prop+'ScopeContent'" :row="scope.row" /></template> </el-table-column></template></el-table></div>
</template><script setup>const props = defineProps({column: {type: Array,default() {return []}},tableData: {type: Array,default() {return []}},
})</script><style lang="scss" scoped></style>

在需要使用自定义 Table 组件的地方引入并使用:

<template>
<base-table :column="tableColumn" :tableData="recordList"> <!-- 刷卡时间自定义内容 --><template #createTimeScopeContent="slotProps" > <span>{{ parseTime(slotProps.row.eventTime) }}</span></template> <!-- // 刷卡时间自定义内容 --><!-- 事件自定义内容 --><template #typeScopeContent="slotProps" >  <dict-tag :options="entrance_type" :value="slotProps.row.event" /></template><!-- // 事件自定义内容 --> <!-- 部门自定义内容 --><template #deptNameScopeContent="slotProps" >  <span>{{ slotProps.row.deptName || "无" }}</span> </template><!-- // 部门自定义内容 -->  <!-- 刷卡地点自定义内容 --><template #controllerNameScopeContent="slotProps" > <dict-tag :options="controller_name" :value="slotProps.row.controllerName" /></template><!-- // 刷卡地点自定义内容 --> 
</base-table> 
</template><script setup>
const tableColumn = ref([{label: "刷卡时间",prop: "createTime",width: "180",align: "center",isScope: true}, {label: "事件",prop: "type",width: "auto",align: "center",isScope: true}, {label: "姓名",prop: "staffName",width: "auto",align: "center"}, {label: "卡号",prop: "cardNo",width: "auto",align: "center"}, {label: "部门",prop: "deptName",width: "auto",align: "center",isScope: true}, {label: "刷卡地点",prop: "controllerName",width: "auto",align: "center",isScope: true}
])
</script>

在 Vue 3 中,你可能会遇到“具名插槽”(Named Slots)的概念,它允许你在组件内部定义特定的插槽位置,并在父组件中为这些具名插槽提供内容。

以下是如何在Vue 3中使用具名插槽的一个基本示例:

  1. 首先,在子组件(ChildComponent.vue)中定义一个具名插槽:
<template><div><h2>这是子组件的标题</h2><slot name="description">默认描述内容</slot><slot name="footer">默认页脚内容</slot></div>
</template>

在这个例子中,我们定义了两个具名插槽:description 和 footer,并且都提供了默认内容。

  1. 然后,在父组件中使用这个子组件并填充具名插槽:
<template><ChildComponent><template v-slot:description><p>这是来自父组件的描述内容</p></template><template v-slot:footer><p>这是来自父组件的页脚内容</p></template></ChildComponent>
</template>

在父组件中,我们使用 v-slot 指令(也可以简写为 #)来指定要填充哪个具名插槽,并在其中提供相应的内容。这样,当渲染子组件时,就会在对应的位置插入这些内容。

vue3 使用具名插槽并且传递值

在 Vue 3 中,具名插槽不仅可以用于传递 HTML 结构,还可以配合作用域插槽(Scoped Slots)来传递数据。作用域插槽允许子组件向其插槽内传递数据,而父组件则可以通过插槽提供的回调函数来访问这些数据并在插槽内进行渲染。

下面是一个使用 Vue 3 中具名插槽结合作用域插槽传递值的例子:

子组件 (ChildComponent.vue):

<template><div><h2>这是子组件的数据列表</h2><!-- 定义作用域插槽,传入一个数组 --><ul><li v-for="item in items" :key="item.id"><slot :item="item" name="listItem"></slot></li></ul></div>
</template><script>
export default {data() {return {items: [{ id: 1, text: '第一条数据' },{ id: 2, text: '第二条数据' },// ...]}}
}
</script>

父组件:

<template><ChildComponent><!-- 使用 v-slot 指令接收子组件传递的 item 数据 --><template #listItem="slotProps"><div><p>ID: {{ slotProps.item.id }}</p><p>内容: {{ slotProps.item.text }}</p><!-- 这里可以根据 item 数据自定义渲染内容 --></div></template></ChildComponent>
</template>

在上述例子中,子组件 ChildComponent 定义了一个具名插槽 listItem 并且每个插槽都绑定了一个 item 数据。

在父组件中,我们通过 v-slot:listItem=“slotProps” 来接收这些数据,并通过 slotProps.item 访问具体的 item 对象属性。这样父组件就可以根据传递过来的数据自行决定如何渲染每一项列表内容了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/10935.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

基于python+vue智慧社区家政服务系统的设计与实现flask-django-nodejs

论文主要是对智慧社区家政服务系统进行了介绍&#xff0c;包括研究的现状&#xff0c;还有涉及的开发背景&#xff0c;然后还对系统的设计目标进行了论述&#xff0c;还有系统的需求&#xff0c;以及整个的设计方案&#xff0c;对系统的设计以及实现&#xff0c;也都论述的比较…

公司内部局域网怎么适用飞书?

随着数字化办公的普及&#xff0c;企业对于内部沟通和文件传输的需求日益增长。飞书作为一款集成了即时通讯、云文档、日程管理、视频会议等多种功能的智能协作平台&#xff0c;已经成为许多企业提高工作效率的首选工具。本文将详细介绍如何在公司内部局域网中应用飞书&#xf…

在 Linux/Ubuntu/Debian 上安装 SQL Server 2019

Microsoft 为 Linux 发行版&#xff08;包括 Ubuntu&#xff09;提供 SQL Server。 以下是有关如何执行此操作的基本指南&#xff1a; 注册 Microsoft Ubuntu 存储库并添加公共存储库 GPG 密钥&#xff1a; sudo wget -qO- https://packages.microsoft.com/keys/microsoft.as…

【GO全栈掌握入门】

GO语言全栈学习咯 ~ 1. GO 语言简介2.语言特性3.哪些公司使用GO语言&#xff1f;3. 安装GO开发环境4. 学习说明&#xff1a;5. GO结构篇5.1 工作空间5.2 导入包5.3 组织结构5.4 依赖管理 6. GO骨肉篇7.GO工具篇 1. GO 语言简介 起源于2007年&#xff0c;GO语言之年轻如你所见&…

【LabVIEW FPGA入门】使用FPGA实现串行同步接口(SSI)

SSI&#xff08;串行同步接口&#xff09;是连接绝对位置传感器和控制器的广泛应用的串行接口。SSI利用控制器发出一个时钟脉冲序列&#xff0c;初始化传感器的门限输出。 传感器不断更新位置数据&#xff0c;并传送到移位寄存器中。在每一个时钟脉冲序列之间&#xff…

Blender小:图片转mesh,并展UV

其实就这么一个插件&#xff1a;Image2Mesh https://github.com/kedepot/i2m-std blender加载完图片之后点这个就行 到了这一步还不行如果直接放到unity中使用的话&#xff0c;添加材质和贴图之后每一个小块都是独立的区域来展示贴图&#xff0c;所以需要展UV