vue前端项目基于element-UI封装分页组件,在所有页面调用

分页组件在前端项目开发中用到的频率非常的高,几乎所有的系统都有所涉及,封装一个分页组件可以帮我们在开发中节省时间,提高开发效率。

在这个示例中,我们使用了ElementUI的Pagination组件,并将其绑定到了一些数据上。你可以根据你的需求自定义这些数据,例如从API获取总页数和当前页码,或者将每页显示的数量作为一个可配置的属性。在handleCurrentChange方法中,你可以根据新的页码更新数据。

下面是封装分页组件代码:

新建一个MyPagination.vue文件

<template>
  <!--  分页组件封装-->
  <div class="TablePage">
    <el-pagination
      :current-page="pageData.page"
      :page-size="pageData.pageSize"
      :total="pageData.total"
      @prev-click="onPrevBut"
      @next-click="onNextBut"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  name: "TablePage",
  props: {
    pageData: Object,
    fatherPage: {
      type: Function,
      default: null,
    },
  },
  },
  methods: {
    // 上一页
    onPrevBut (page) {
      this.parent(page)
    },
    // 下一页
    onNextBut (page) {
      this.parent(page)
    },
    // 给父组件传值
    parent (page) {
      this.fatherPage(page)
    },
  },
};

在使用这个组件的地方,导入这个组件并将其作为一个标签使用,例如



<template>
  <div>
    其它代码。。。
   <my-pagination :page-data="pageData" :father-page="fatherPage" ></my-pagination>
  </div>
</template>
<script>
import my-pagination from '../components/MyPagination.vue/'  //这里路径请以实际的文件的路径为准
export default{
components:{my-pagination},
data(){
    return {
 pageData: {
        // 页码
        page: 1,
        // 一页的数据个数
        pageSize: 10,
        // 总数
        total: 0
      },
},
methods:{

/*

在这里还要写上API请求函数,也就是从后端返回的你需要渲染的数据

例如请求的结果为res
 this.pageData.total = res.length
*/
 fatherPage (e) {
      this.pageData.page = e
    },

}
}

</script>


 

猜你喜欢

转载自blog.csdn.net/qq_46103732/article/details/130323884