vue3使用element-plus再次封裝table組件的基本步驟
vue3 使用element-plus 如何再次封裝table組件
基本步驟
- 創(chuàng)建子組件
- 默認數(shù)據(jù)配置
- 在需要使用自定義 Table 組件的地方引入并使用:
創(chuàng)建子組件:
創(chuàng)建一個新的 .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中使用具名插槽的一個基本示例:
- 首先,在子組件(ChildComponent.vue)中定義一個具名插槽:
<template> <div> <h2>這是子組件的標題</h2> <slot name="description">默認描述內容</slot> <slot name="footer">默認頁腳內容</slot> </div> </template>
在這個例子中,我們定義了兩個具名插槽:description 和 footer,并且都提供了默認內容。
- 然后,在父組件中使用這個子組件并填充具名插槽:
<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)來傳遞數(shù)據(jù)。作用域插槽允許子組件向其插槽內傳遞數(shù)據(jù),而父組件則可以通過插槽提供的回調函數(shù)來訪問這些數(shù)據(jù)并在插槽內進行渲染。
下面是一個使用 Vue 3 中具名插槽結合作用域插槽傳遞值的例子:
子組件 (ChildComponent.vue):
<template> <div> <h2>這是子組件的數(shù)據(jù)列表</h2> <!-- 定義作用域插槽,傳入一個數(shù)組 --> <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: '第一條數(shù)據(jù)' }, { id: 2, text: '第二條數(shù)據(jù)' }, // ... ] } } } </script>
父組件:
<template> <ChildComponent> <!-- 使用 v-slot 指令接收子組件傳遞的 item 數(shù)據(jù) --> <template #listItem="slotProps"> <div> <p>ID: {{ slotProps.item.id }}</p> <p>內容: {{ slotProps.item.text }}</p> <!-- 這里可以根據(jù) item 數(shù)據(jù)自定義渲染內容 --> </div> </template> </ChildComponent> </template>
在上述例子中,子組件 ChildComponent 定義了一個具名插槽 listItem 并且每個插槽都綁定了一個 item 數(shù)據(jù)。
在父組件中,我們通過 v-slot:listItem=“slotProps” 來接收這些數(shù)據(jù),并通過 slotProps.item 訪問具體的 item 對象屬性。這樣父組件就可以根據(jù)傳遞過來的數(shù)據(jù)自行決定如何渲染每一項列表內容了。
以上就是vue3使用element-plus再次封裝table組件的基本步驟的詳細內容,更多關于vue3 element-plus再次封裝table的資料請關注腳本之家其它相關文章!
- vue3?element?plus?table?selection展示數(shù)據(jù),默認選中功能方式
- element-plus的el-table自定義表頭篩選查詢功能實現(xiàn)
- Vue3+Element-Plus使用Table預覽圖片發(fā)生元素遮擋的解決方法
- vue3使用elementPlus進行table合并處理的示例詳解
- vue3+element Plus實現(xiàn)在table中增加一條表單數(shù)據(jù)的示例代碼
- vue3+elementplus基于el-table-v2封裝公用table組件詳細代碼
- Vue3中Element Plus Table(表格)點擊獲取對應id方式
- vue3 elementplus table合并寫法
- Element?UI/Plus中全局修改el-table默認樣式的解決方案
- ElementPlus?Table表格實現(xiàn)可編輯單元格