페이징 구성 요소는 프론트 엔드 프로젝트 개발에서 매우 자주 사용되며 거의 모든 시스템이 관련되어 있습니다.페이징 구성 요소를 캡슐화하면 개발 시간을 절약하고 개발 효율성을 높일 수 있습니다.
이 예제에서는 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>