简体   繁体   English

如何更改 jquery Deferred 以使用 Vue nexttick?

[英]How to change jquery Deferred to use Vue nexttick?

I need to change this as I have read nextTick is a better option compared to deferred.我需要更改这一点,因为我已阅读 nextTick 与 deferred 相比是一个更好的选择。

This is the original code that works well:这是运行良好的原始代码:

methods: {
    ...mapActions(['fetchOverdraftLogs', 'setOverdraftFilters', 'fetchUserEventsCsvFile']),
},
  computed: {
    ...mapGetters(['getOverdraftLogs'])
  },
...
        loadData: filters => {
          const d = $.Deferred()
          this.fetchOverdraftLogs({ params: this.filtersToSend(filters) })
            .then(res => {
              d.resolve(this.getOverdraftLogs)
              this.setFilterValues()
            })
          return d.promise()
        })
        }

However, I am not able to rework this with nextTick:但是,我无法使用 nextTick 进行修改:

loadData: filters => {
  this.fetchOverdraftLogs({ params: this.filtersToSend(filters) })
  this.$nextTick(() => {
    this.setFilterValues()
    this.getOverdraftLogs
  })
}

I need to make sure for the grid that after the API is called it reads the data from the state.我需要确保网格在调用 API 后从 state 读取数据。

Could you assist?你能帮忙吗?

Deferred pattern is generally an antipattern, especially with ES promises.延迟模式通常是一种反模式,尤其是 ES 承诺。 It's not an alternative to nextTick .它不是nextTick的替代品。 Promises are already asynchronous, a promise that is instantly resolved provides a small delay. Promise 已经是异步的,立即解决的 promise 提供了一个小的延迟。 nextTick provides a delay that is big enough DOM to be updated. nextTick提供了一个足够大的延迟来更新 DOM。

nextTick supports promises but here it's not needed. nextTick支持 Promise,但这里不需要。

It should be:它应该是:

    loadData: filters => {
      return this.fetchOverdraftLogs({ params: this.filtersToSend(filters) })
        .then(() => {
          this.setFilterValues()
          return this.getOverdraftLogs;
        })
    }

Vuex actions aren't supposed to return data they operate on, so there should be no res . Vuex 操作不应该返回他们操作的数据,所以不应该有res

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM