欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3使用element-plus再次封裝table組件的基本步驟

 更新時間:2024年03月22日 10:14:04   作者:Software攻城獅  
這篇文章主要介紹了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的資料請關注腳本之家其它相關文章!

相關文章

  • vue為自定義路徑設置別名的方法

    vue為自定義路徑設置別名的方法

    這篇文章介紹了vue為自定義路徑設置別名的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-11-11
  • Vue+Vant實現(xiàn)下拉加載功能

    Vue+Vant實現(xiàn)下拉加載功能

    為了像微信一樣方便地加載更多歷史消息,這篇文章將為大家介紹我們如何使用vant組件來實現(xiàn)下拉加載功能,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-06-06
  • Vue3實現(xiàn)登錄表單驗證功能

    Vue3實現(xiàn)登錄表單驗證功能

    這篇文章主要介紹了Vue3實現(xiàn)登錄表單驗證功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • React?Diff算法不采用Vue的雙端對比原因詳解

    React?Diff算法不采用Vue的雙端對比原因詳解

    這篇文章主要介紹了React?Diff算法不采用Vue雙端對比算法原因詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • VUE動態(tài)生成word的實現(xiàn)

    VUE動態(tài)生成word的實現(xiàn)

    這篇文章主要介紹了VUE動態(tài)生成word的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vue.js數(shù)字輸入框組件使用方法詳解

    Vue.js數(shù)字輸入框組件使用方法詳解

    這篇文章主要為大家詳細介紹了Vue.js數(shù)字輸入框組件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 解決vue路由name同名,路由重復的問題

    解決vue路由name同名,路由重復的問題

    這篇文章主要介紹了解決vue路由name同名,路由重復的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue使用websocket連接優(yōu)化性能方式

    vue使用websocket連接優(yōu)化性能方式

    這篇文章主要介紹了vue使用websocket連接優(yōu)化性能方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue.set 全局操作簡單示例

    Vue.set 全局操作簡單示例

    這篇文章主要介紹了Vue.set 全局操作,結合簡單實例形式分析了Vue.set 全局操作相關使用技巧與注意事項,需要的朋友可以參考下
    2019-09-09
  • vue2使用keep-alive緩存多層列表頁的方法

    vue2使用keep-alive緩存多層列表頁的方法

    今天小編就為大家分享一篇vue2使用keep-alive緩存多層列表頁的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09

最新評論