1. 程式人生 > 其它 >VUEX已獲取到非同步資料 元件頁面獲取不到(沒有資料)的問題

VUEX已獲取到非同步資料 元件頁面獲取不到(沒有資料)的問題

技術標籤:前端vuexvue

使用VUEX的action從遠端獲取資料,store/modules/index.js

async getStatistics({ commit }, val) {
    const { code, msg, data } = await getStatistics({
      start: val.start,
    })

元件上用dispatch呼叫,views/index/index.vue:

this.$store
        .dispatch('index/getStatistics', {
          start: this. start,
}) .then(() => { this.start++ })

並在computed裡用…mapGetters獲取資料:

...mapGetters({
        statistics: 'index/statistics',
      }),

開始怎麼也獲取不到,vuex資料不是響應式的嗎,為什麼不行呢?
後來試了試,如果{{ a }} 這樣使用是可以的,在js裡使用就得等到dispatch獲取到資料以後才行,所以,如果要在js裡使用資料,就要把用到資料的程式碼放到了dispatch的then裡面才可以。

歡迎高手交流指點!我的微信: