Vuex: Proposal: let the actions function first param context is configurable

Created on 4 Aug 2017  路  2Comments  路  Source: vuejs/vuex

What problem does this feature solve?

when I use axios in many modules

// each module has to import
import axios from 'axios'
export default {
  ...
  actions: {
    getUsers ({ commit }) {
      axios.get('/users').then(data => {
        commit('users', data)
      })
    }
  }
}

What does the proposed API look like?

If the actions first param context is configurable, it could be like this,
the axios may be db, model or socket

// in store/index
import axios from 'axios'
const store = new Vuex.Store({
  context: {
    axios
  },
  ...
  modules: {
    ...
  }
})


// in module/user
export default {
  ...,
  actions: {
    getUsers ({ axios, commit }) {
      axios.get('/users').then(data => {
        commit('users', data)
      })
    }
  }
}

I read src/store.js and find that the context is not configurable.

Most helpful comment

There was this kind of discussion in the past and we didn't introduce the feature that can be inject something in the action context. See #571

You can use the action enhancer approach to do that.

All 2 comments

There was this kind of discussion in the past and we didn't introduce the feature that can be inject something in the action context. See #571

You can use the action enhancer approach to do that.

I read #571, thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dsl101 picture dsl101  路  20Comments

ruiposse picture ruiposse  路  27Comments

pschaub picture pschaub  路  24Comments

terion-name picture terion-name  路  19Comments

CodinCat picture CodinCat  路  38Comments