前言:element Ui 分頁size-change事件觸發同時也出發了current-change事件
bug:當選擇最後至一頁切換條數就會請求兩次接口
1:選擇最後一頁(第2頁)
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN2XjlGcjAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL4tmaNNTSq1EMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLwYjM1UjNwMTM3ATOwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2:切換條數會請求兩次接口
解決方法:定義變量state預設為true;條數改變的時候@current-change事件則不請求接口,這樣的話隻請求了@size-change事件的接口請求、
<el-pagination center background :pager-count="5" layout="prev, pager, next, sizes" :page-sizes="[10,20,50]"
:page-size="pagesize" :total="total" @current-change="handleCurrentChange" @size-change="handleSizeChange">
</el-pagination>
data(){
return{
dataObj: {
token: this.$store.getters.token,
index: 1,
length: 10
},
total:0,
currpage: 1,
pagesize: 10,
total:0,//條數的總數
state:true,
}
}
methods:{
getData() {
接口請求....
api().then(res=>{
請求成功...
this.state=true;
this.total = res.max; //條數的總數
})
},
handleCurrentChange(cpage) { //頁數
// console.log(cpage)
this.currpage = cpage;
this.dataObj.index = cpage;
if(this.state==true){
this.getData()
}
},
handleSizeChange(psize) { //條數
// console.log(psize)
this.state=false;
this.pagesize = psize;
this.dataObj.length = psize
if(this.state==false){
this.getData()
}
},
}