TS + vue3 + element-plus 分页组件封装

记录下封装分页组件的小坑,由于开始子组件使用的:current-page来绑定,切换页码的时候页码视图没有变化,导致以为是子组件无法监听到父组件的数据变化导致的,后面查看文档使用v-model="current_page"视图就可以变化了

父组件
 <div class="pagination">
        <pagination
          :current-page="changePage.currentPage"
          :page-sizes="[5, 15, 20, 25, 30, 35]"
          :total="userStore.userList.total"
          @current-change="handleCurrentChange"
        />
<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";

import { useUserList } from "@/store/userList";
import pagination from "@/components/pagination.vue";
const userStore = useUserList();
const changePage = reactive<pagination_type>({
  currentPage: 1,
  limit: 10,
});
onMounted(async () => {
  await userStore.getUserListApi(changePage);
});

const handleCurrentChange = async (val: number) => {
  changePage.currentPage = val;
  await userStore.getUserListApi(changePage);
};
</script>
子组件
<template>
  <div>
    <el-pagination
      background
      :layout="layout"
      v-model="current_page"
      :page-sizes="page_sizes"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { toRefs} from "vue"
type Props = {
  layout?: string;
  current_page?: number;
  page_sizes?: number[];
  total?: number;
};
const propData = withDefaults(defineProps<Props>(), {
  layout: "prev, pager, next",
  current_page: 1,
  page_sizes: () => [5, 15, 20, 25, 30, 35],
  total: 0,
});
const { current_page } = toRefs(propData) 

const emit = defineEmits(["currentChange"]);

const handleCurrentChange = (val: number) => {
  emit("currentChange", val);
};


</script>
好的,我来为您介绍一下如何在Vue3项目中结合Element Plus和TypeScript来封装一个带有分页功能的表格组件。 首先,我们需要创建一个新的Vue组件,比如叫做TableWithPagination.vue。 在组件的模板部分,我们可以这样写: ```vue <template> <div> <el-table :data="tableData" style="width: 100%"> <el-table-column v-for="column in columns" :key="column.prop" :prop="column.prop" :label="column.label" /> </el-table> <el-pagination @current-change="handlePageChange" :current-page="currentPage" :page-size="pageSize" layout="total, prev, pager, next, jumper" :total="total" /> </div> </template> ``` 在脚本部分,我们需要这样写: ```vue <script lang="ts" setup> import { ref, computed } from &#39;vue&#39;; import { ElTable, ElTableColumn, ElPagination } from &#39;element-plus&#39;; interface Column { prop: string; label: string; } interface Props { data: any[]; columns: Column[]; pageSize?: number; } const props = withDefaults(defineProps<Props>(), { pageSize: 10, }); const tableData = computed(() => { const start = (props.currentPage - 1) * props.pageSize; return props.data.slice(start, start + props.pageSize); }); const total = computed(() => props.data.length); const currentPage = ref(1); const handlePageChange = (page: number) => { currentPage.value = page; }; </script> ``` 在样式部分,我们可以根据需要添加一些样式: ```vue <style scoped> /* 添加一些自定义样式 */ </style> ``` 这个组件接收三个主要属性: 1. data: 表格数据数组 2. columns: 表格列配置数组 3. pageSize: 每页显示的数据条数(可选,默认为10) 组件内部使用 computed 计算属性来计算当前页需要显示的数据和总数据量。使用 ref 定义当前页码,并提供一个 handlePageChange 方法来处理页码变化。 使用这个组件时,我们可以这样写: ```vue <template> <TableWithPagination :data="tableData" :columns="columns" /> </template> <script lang="ts" setup> import TableWithPagination from &#39;./components/TableWithPagination.vue&#39;; const tableData = ref([...]); const columns = ref([...]); </script> ``` 这样,我们就完成了一个简单的Vue3 + Element Plus + TypeScript的表格分页组件封装
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值