Home  >  Q&A  >  body text

javascript - How to use loading in vue action

This is how the loading state is generally used in Vue

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

But how to use it in the vuex action? The following example uses a public loading and found that it does not work. The loading state should be local. So how to control the loading state in vuex?

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

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-19 10:22:47

    Following your second example,
    put the loading flag in the state, and the component gets whether it is loading through the state

    reply
    0
  • Cancelreply