当前位置: 代码网 > it编程>编程语言>Javascript > vue3使用element-plus再次封装table组件的基本步骤

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

2024年05月18日 Javascript 我要评论
vue3 使用element-plus 如何再次封装table组件基本步骤创建子组件默认数据配置在需要使用自定义 table 组件的地方引入并使用:创建子组件:创建一个新的 .vue 文件,例如子组件

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的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com