Vuex每秒钟提交一次相同的动作很慢

时间:2018-10-09 07:42:10

标签: vuejs2 vuex

我正在将Vuex V3.0.1与Vue.js v 2.5.17一起使用

vuex中的同一动作提交恰好每秒钟变慢一次。如果我将相同的axios请求放入我的组件中,然后从该组件内部调用它,则该请求始终在16到22毫秒之间。但是,当我分派到商店时,每隔一秒钟的请求就会持续超过300毫秒。

Chrome Network Traffic

this.$store.dispatch('getQueryData', {QueryId: this.to.QueryId, params: data, CacheResults : this.to.CacheResults }).then(response => {
    this.options = response.data
        var arr = response.data
        var defaultValue = this.model[this.field.key] || this.field.defaultValue || null
        var valueprop = this.to.valueProp
        var model = {}
        if (defaultValue !== null)
        {
          this.options.forEach(function(value){
              if (value[valueprop] == defaultValue){
                model = value
              }
          })
          this.model[this.field.key] = model
        }
}, error => {
   this.$store.commit('setError', error.response)
   this.options = [];
})

在我的商店里

getQueryData ({commit}, payload) {
  const QueryIdStr = String(payload.QueryId)

  if (store.state.queries[QueryIdStr]) {
    var promise = Promise.resolve({data: store.state.queries[QueryIdStr]})
    return promise
  } else {
    return new Promise((resolve, reject) => {
      axios
        .get('/scadservices/api/tablesv2/Query/' + payload.QueryId + '/data.json', {crossdomain: true, withCredentials: true, params: payload.params})
        .then(response => {
          var output = {
            QueryId: payload.QueryId,
            data: response.data
          }
          if (payload.CacheResults)
          {
            commit('setQueryData', output)
          }
          resolve(response)
        }, error => {
          commit('setError', error.response)
          reject(error)
        })
    })
  }
},

还有我的突变

setQueryData: (state, payload) => {
  const QueryIdStr = String(payload.QueryId)
  Vue.set(state.queries, QueryIdStr, payload.data)
}

0 个答案:

没有答案