avatar
fireworks99
keep hungry keep foolish

elementUI .sync

elementUI的分页组件el-pagination,有一个属性叫current-page,由调用它的组件(即父组件)通过props传给它(el-pagination)。

有一种情境:数据很多时,当前有多页(1~N),1 < current-page <= N,当一次点击使数据变得很少以至于只有一页数据时,el-pagination组件把当前页码置为1,又因为current-page是父组件的数据,所以只能由父组件来修改,即要么添加.sync修饰符,要么向父组件发送事件父组件在methods里自己改。

如果什么都不做,那么current-page还是原先那个大于1的数字(比如是2),但此时子组件里当前页码是1,产生了冲突。假如此时一次点击使数据变得较多产生了很多页,那么当前页码是1,但是current-page是2,此时用户点击页码2想跳转到第二页,但是点击之后current-page被复制为2,子组件发现current-page被赋值前后没有变化,故页码2不会被高亮,页码1仍高亮。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <!-- 引入样式 --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="app"> <el-button type="primary" @click="changeTotal()">改变记录数目</el-button> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-sizes="[20, 50, 100, 200]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination> </div> <script> const app = new Vue({ el: "#app", data() { return { currentPage: 1, total: 800 } }, methods: { handleSizeChange(val) { }, handleCurrentChange(val) { this.currentPage = val; }, changeTotal() { this.total = this.total === 800 ? 0 : 800; } } }) </script> </body> </html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy