vue3使用element-plus再次封装table组件的基本步骤

 更新时间:2024年03月22日 10:14:04   作者:Software攻城狮  
这篇文章主要介绍了vue3使用element-plus再次封装table组件的基本步骤,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

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中使用具名插槽的一个基本示例:

  • 首先,在子组件(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)来传递数据。作用域插槽允许子组件向其插槽内传递数据,而父组件则可以通过插槽提供的回调函数来访问这些数据并在插槽内进行渲染。

下面是一个使用 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 对象属性。这样父组件就可以根据传递过来的数据自行决定如何渲染每一项列表内容了。

以上就是vue3使用element-plus再次封装table组件的基本步骤的详细内容,更多关于vue3 element-plus再次封装table的资料请关注脚本之家其它相关文章!

相关文章

  • vue为自定义路径设置别名的方法

    vue为自定义路径设置别名的方法

    这篇文章介绍了vue为自定义路径设置别名的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-11-11
  • Vue+Vant实现下拉加载功能

    Vue+Vant实现下拉加载功能

    为了像微信一样方便地加载更多历史消息,这篇文章将为大家介绍我们如何使用vant组件来实现下拉加载功能,感兴趣的小伙伴可以跟随小编一起了解一下
    2023-06-06
  • Vue3实现登录表单验证功能

    Vue3实现登录表单验证功能

    这篇文章主要介绍了Vue3实现登录表单验证功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • React Diff算法不采用Vue的双端对比原因详解

    React Diff算法不采用Vue的双端对比原因详解

    这篇文章主要介绍了React Diff算法不采用Vue双端对比算法原因详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • VUE动态生成word的实现

    VUE动态生成word的实现

    这篇文章主要介绍了VUE动态生成word的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Vue.js数字输入框组件使用方法详解

    Vue.js数字输入框组件使用方法详解

    这篇文章主要为大家详细介绍了Vue.js数字输入框组件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • 解决vue路由name同名,路由重复的问题

    解决vue路由name同名,路由重复的问题

    这篇文章主要介绍了解决vue路由name同名,路由重复的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • vue使用websocket连接优化性能方式

    vue使用websocket连接优化性能方式

    这篇文章主要介绍了vue使用websocket连接优化性能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    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

最新评论