首页  >  问答  >  正文

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 天前481

全部回复(1)我来回复

  • phpcn_u1582

    phpcn_u15822017-05-19 10:22:47

    承接你的第二个例子,
    把loading的flag放在state中,组件通过state获取是否loading

    回复
    0
  • 取消回复