vue3使用element-plus再次封裝table組件的基本步驟
vue3 使用element-plus 如何再次封裝table組件
基本步驟
- 創(chuàng)建子組件
- 默認(rèn)數(shù)據(jù)配置
- 在需要使用自定義 Table 組件的地方引入并使用:
創(chuàng)建子組件:
創(chuàng)建一個(gè)新的 .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">
<!-- 刷卡時(shí)間自定義內(nèi)容 -->
<template #createTimeScopeContent="slotProps" >
<span>{{ parseTime(slotProps.row.eventTime) }}</span>
</template>
<!-- // 刷卡時(shí)間自定義內(nèi)容 -->
<!-- 事件自定義內(nèi)容 -->
<template #typeScopeContent="slotProps" >
<dict-tag :options="entrance_type" :value="slotProps.row.event" />
</template>
<!-- // 事件自定義內(nèi)容 -->
<!-- 部門自定義內(nèi)容 -->
<template #deptNameScopeContent="slotProps" >
<span>{{ slotProps.row.deptName || "無(wú)" }}</span>
</template>
<!-- // 部門自定義內(nèi)容 -->
<!-- 刷卡地點(diǎn)自定義內(nèi)容 -->
<template #controllerNameScopeContent="slotProps" >
<dict-tag :options="controller_name" :value="slotProps.row.controllerName" />
</template>
<!-- // 刷卡地點(diǎn)自定義內(nèi)容 -->
</base-table>
</template>
<script setup>
const tableColumn = ref([
{
label: "刷卡時(shí)間",
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: "卡號(hào)",
prop: "cardNo",
width: "auto",
align: "center"
}, {
label: "部門",
prop: "deptName",
width: "auto",
align: "center",
isScope: true
}, {
label: "刷卡地點(diǎn)",
prop: "controllerName",
width: "auto",
align: "center",
isScope: true
}
])
</script>
在 Vue 3 中,你可能會(huì)遇到“具名插槽”(Named Slots)的概念,它允許你在組件內(nèi)部定義特定的插槽位置,并在父組件中為這些具名插槽提供內(nèi)容。
以下是如何在Vue 3中使用具名插槽的一個(gè)基本示例:
- 首先,在子組件(ChildComponent.vue)中定義一個(gè)具名插槽:
<template>
<div>
<h2>這是子組件的標(biāo)題</h2>
<slot name="description">默認(rèn)描述內(nèi)容</slot>
<slot name="footer">默認(rèn)頁(yè)腳內(nèi)容</slot>
</div>
</template>
在這個(gè)例子中,我們定義了兩個(gè)具名插槽:description 和 footer,并且都提供了默認(rèn)內(nèi)容。
- 然后,在父組件中使用這個(gè)子組件并填充具名插槽:
<template>
<ChildComponent>
<template v-slot:description>
<p>這是來(lái)自父組件的描述內(nèi)容</p>
</template>
<template v-slot:footer>
<p>這是來(lái)自父組件的頁(yè)腳內(nèi)容</p>
</template>
</ChildComponent>
</template>
在父組件中,我們使用 v-slot 指令(也可以簡(jiǎn)寫(xiě)為 #)來(lái)指定要填充哪個(gè)具名插槽,并在其中提供相應(yīng)的內(nèi)容。這樣,當(dāng)渲染子組件時(shí),就會(huì)在對(duì)應(yīng)的位置插入這些內(nèi)容。
vue3 使用具名插槽并且傳遞值
在 Vue 3 中,具名插槽不僅可以用于傳遞 HTML 結(jié)構(gòu),還可以配合作用域插槽(Scoped Slots)來(lái)傳遞數(shù)據(jù)。作用域插槽允許子組件向其插槽內(nèi)傳遞數(shù)據(jù),而父組件則可以通過(guò)插槽提供的回調(diào)函數(shù)來(lái)訪問(wèn)這些數(shù)據(jù)并在插槽內(nèi)進(jìn)行渲染。
下面是一個(gè)使用 Vue 3 中具名插槽結(jié)合作用域插槽傳遞值的例子:
子組件 (ChildComponent.vue):
<template>
<div>
<h2>這是子組件的數(shù)據(jù)列表</h2>
<!-- 定義作用域插槽,傳入一個(gè)數(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>內(nèi)容: {{ slotProps.item.text }}</p>
<!-- 這里可以根據(jù) item 數(shù)據(jù)自定義渲染內(nèi)容 -->
</div>
</template>
</ChildComponent>
</template>
在上述例子中,子組件 ChildComponent 定義了一個(gè)具名插槽 listItem 并且每個(gè)插槽都綁定了一個(gè) item 數(shù)據(jù)。
在父組件中,我們通過(guò) v-slot:listItem=“slotProps” 來(lái)接收這些數(shù)據(jù),并通過(guò) slotProps.item 訪問(wèn)具體的 item 對(duì)象屬性。這樣父組件就可以根據(jù)傳遞過(guò)來(lái)的數(shù)據(jù)自行決定如何渲染每一項(xiàng)列表內(nèi)容了。
以上就是vue3使用element-plus再次封裝table組件的基本步驟的詳細(xì)內(nèi)容,更多關(guān)于vue3 element-plus再次封裝table的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式
- element-plus的el-table自定義表頭篩選查詢功能實(shí)現(xiàn)
- Vue3+Element-Plus使用Table預(yù)覽圖片發(fā)生元素遮擋的解決方法
- vue3使用elementPlus進(jìn)行table合并處理的示例詳解
- vue3+element Plus實(shí)現(xiàn)在table中增加一條表單數(shù)據(jù)的示例代碼
- vue3+elementplus基于el-table-v2封裝公用table組件詳細(xì)代碼
- Vue3中Element Plus Table(表格)點(diǎn)擊獲取對(duì)應(yīng)id方式
- vue3 elementplus table合并寫(xiě)法
- Element?UI/Plus中全局修改el-table默認(rèn)樣式的解決方案
- ElementPlus?Table表格實(shí)現(xiàn)可編輯單元格
相關(guān)文章
Vue3實(shí)現(xiàn)登錄表單驗(yàn)證功能
這篇文章主要介紹了Vue3實(shí)現(xiàn)登錄表單驗(yàn)證功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
React?Diff算法不采用Vue的雙端對(duì)比原因詳解
這篇文章主要介紹了React?Diff算法不采用Vue雙端對(duì)比算法原因詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
VUE動(dòng)態(tài)生成word的實(shí)現(xiàn)
這篇文章主要介紹了VUE動(dòng)態(tài)生成word的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
解決vue路由name同名,路由重復(fù)的問(wèn)題
這篇文章主要介紹了解決vue路由name同名,路由重復(fù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue2使用keep-alive緩存多層列表頁(yè)的方法
今天小編就為大家分享一篇vue2使用keep-alive緩存多層列表頁(yè)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

