vue+ elementUI 自封装分页插件

elementUI提供了一套很好的分页解决方案,但是项目中很多地方使用,每次要写重复代码,不够解耦合,于是考虑把分页单独提出来作为一个公共模块,使用时直接调用,代码更简洁好看

一.封装Pagination:

新建一个Pagination.vue页面,代码如下

<template>
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[5, 10, 20, 40]"
    :page-size="pagesize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  ></el-pagination>
</template>
<script>
export default {
  name: "Pagination",
  props: {
    url: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      total: 0,
      pagesize: 5,
      currentPage: 1
    };
  },
  mounted() {
    this.reload();
  },
  methods: {
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function(size) {
      this.pagesize = size;
      this.reload();
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
      this.reload();
    },
    reload() {
      this.$Axios.post(
        this.url,
        { page: this.currentPage, size: this.pagesize },
        res => {
          if (res.data.success) {
            this.$emit("childByValue", res.data.data);
            this.total = res.data.count;
          } else {
            this.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        }
      );
    }
  }
};
</script>

说明如下:1.JS中this.$Axios.post换成你自己的访问后台api方法

                  2.参数page,size也是你自己后台定义的接受分页参数名称

                  3.返回类型res结构相应要改成你自己返回数据的格式

二.页面处调用

1.引入Pagination.vue

import Pagination from "@/components/Pagination.vue";
components: {
    Pagination
  }

2.调用

 <Pagination url="user/query" v-on:childByValue="childByValue"/>

调用说明:1.url对你的你请求后台的路径

                  2.v-on接受 Pagination传递过来的childByValue值

3.定义方法,接受childByValue值

childByValue(childValue) {
      this.usersData = childValue;
    }

此处userData是你页面接受展示table页面是数组

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些基本的代码模板。 首先,您需要在Vue项目中安装Element UI并导入相关组件。你可以使用npm安装Element UI: ``` npm i element-ui -S ``` 接下来,您需要在vue组件中导入Element UI的分页组件: ```javascript <template> <div> <el-table :data="tableData"> // 表格列 </el-table> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination> </div> </template> <script> import { Table, Pagination } from 'element-ui'; export default { components: { Table, Pagination }, data() { return { tableData: [], currentPage: 1, pageSize: 10, total: 0 } }, methods: { fetchData() { // 向后端请求数据 }, handleSizeChange(val) { this.pageSize = val; this.fetchData(); }, handleCurrentChange(val) { this.currentPage = val; this.fetchData(); } }, mounted() { this.fetchData(); } } </script> ``` 在上述代码中,`Table`和`Pagination`是导入的组件,`tableData`是表格数据,`currentPage`和`pageSize`是当前页码和每页显示的数据条数,`total`是总数据量。在`el-pagination`标签中,我们绑定了`size-change`和`current-change`事件,分别对应每页显示的数据条数和当前页码的改变。在`handleSizeChange`和`handleCurrentChange`方法中,我们可以进行数据请求,刷新表格数据。 当然,上述代码只是一个简单的示例,您需要根据您的具体需求进行相应的改动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值