首頁  >  問答  >  主體

javascript - vue如何在action中使用loading

在Vue中一般這樣使用loading狀態

getData(){
    this.loading = true;
    get(api).then(res => {
        this.data = res;
        this.loading = false;
    })
}

但在vuex的action中如何使用,下面範例使用了一個公開的loading發現不行,loading狀態應該是局部的,那麼如何在vuex中控制loading狀態?

  const actions = {
  getProductInfo({commit}){
    commit(types.LOADING, true)
    api.xxx()
      .then(res => {
        commit(types.PRODUCTINFO, res.data)
        commit(types.LOADING, false)
      })
  },
世界只因有你世界只因有你2684 天前483

全部回覆(1)我來回復

  • phpcn_u1582

    phpcn_u15822017-05-19 10:22:47

    承接你的第二個例子,
    把loading的flag放在state中,元件透過state取得是否loading

    回覆
    0
  • 取消回覆