element-ui分页组件修改当前页current-page后current-change事件不触发的一个小bug
最近在开发中遇到一个element-ui分页的一个小bug,具体场景如下
1、首先点击正常分页来到第二页数据正常展示
2、点击搜索,需求为回到第一页按条件搜索,此时也正常展示
3、再次点击分页去第二页,此时分页还停留在第一页,current-change事件不触发
原因有可能是点击搜索的时候在搜索里面通过代码去改变了current-page让分页回到第一页,但有可能element内部的页码数据未改变,因此当在此点击第二页的时候相当于页码未发生改变,current-change事件不触发(纯属个人想法)
解决方法
给分页加上一个控制显示和隐藏的变量 v-if="pageshow"
-
<div class="pagination" v-if="pageshow">
-
<el-pagination
-
@size-change="handleSizeChange"
-
@current-change="handleCurrentChange"
-
:current-page.sync="cur_page"
-
:page-size="10"
-
layout="total, prev, pager, next, jumper"
-
:total="recordsTotal">
-
</el-pagination>
-
</div>
在搜索改变页码的时候加上如下代码
-
this.cur_page = 1;//cur_page 当前页
-
this.getData();//获取数据
-
this.pageshow = false;//让分页隐藏
-
this.$nextTick(() => {//重新渲染分页
-
this.pageshow = true;
-
});
也就是重新去渲染一遍分页组件,问题解决。