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

fnlctrl picture fnlctrl  路  4Comments

blocka picture blocka  路  4Comments

visualjerk picture visualjerk  路  3Comments

gdelazzari picture gdelazzari  路  3Comments

gorbypark picture gorbypark  路  3Comments