【问题标题】:Vue.js - Watch data changesVue.js - 观察数据变化
【发布时间】:2019-03-01 20:40:13
【问题描述】:

我在我的应用程序中添加了一个 Bootstrap Vue 表,并希望在用户使用内置分页切换到另一个页面时进行记录。

https://bootstrap-vue.js.org/docs/components/table/

为此,我添加了一个@change,它应该用于监听和记录分页的 v-model,currentPage。但是,它似乎在currentPage 实际更新之前被调用。

如何正确收听此更新?

这是我尝试过的: https://jsfiddle.net/eywraw8t/394424/

new Vue({
  el: "#app",
  data: {
   totalRows: 50,
   perPage: 10,
   currentPage: 1
  },
  methods: {
  	pagination() {
    	console.log(this.currentPage);
    },
  }
})
<div id="app">
  <div>
    <b-pagination :total-rows="totalRows" :per-page="perPage" v-model="currentPage" @change="pagination" />
  </div>
</div>

【问题讨论】:

    标签: javascript vue.js bootstrap-vue


    【解决方案1】:

    你可以看currentPage

    new Vue({
      el: "#app",
      data: {
       totalRows: 50,
       perPage: 10,
       currentPage: 1
      },
      watch: {
        currentPage: function (newVal) {
           console.log('Change to page', newVal)
        }
      },
      methods: {
        setTo() {
         this.currentPage = 1;
         console.log(this.currentPage);
        }
      }
    })
    

    Check demo here

    【讨论】:

    • 谢谢,这正是我想要的。您是否知道在性能方面是使用console.log(newVal) 还是只在函数中使用console.log(this.currentPage) 更好? jsfiddle.net/Sirence/pt3jmbzh/3
    • 我认为这两种方式都行。我更喜欢console.log(newVal)
    猜你喜欢
    • 2012-02-22
    • 2019-01-24
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多