-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject axios into store instance #26
Comments
Hi @alidcastano . Actually i was and still trying to find a better way for usage in actions. But unfortunately it is not possible as vuex internally handles dispatch/commits. I will investigate more on possible solutions. |
I ended up doing this, which actually isn't a terrible way to do it:
|
Agree terrible state :)) I'm trying to use vuex plugins (https://vuex.vuejs.org/en/plugins.html) injecting on every action. |
Are you referring to the nuxt auth module? |
Not necessary auth module. There should be a common solution for that :)) |
@pi0: Is there any progress with this, or other suggestions on how to achieve it? This module is so close to what I need! I need axios access throughout my vuex stores and would rather not pass through the instance into the action. |
I've been experimenting with vuex plugins as mentioned earlier in the issue by @pi0. However, when setting up the plugin one does have access to the store object. So I experimented with the following with some success. (it does seem hackish though) Updated function axiosInjectPlugin() {
return store => {
store.getters['$axios'] = () => {
return store.$axios
}
}
}
export const plugins = [
axiosInjectPlugin()
]
export const actions = {
// for standard stores, use getters
login (ctx, { username, password, role}) {
let $axios = ctx.getters['$axios']()
}
// for module based stores, use rootGetters
login2 (ctx, { username, password, role}) {
let $axios = ctx.rootGetters['$axios']()
}
} A couple of notes:
Any suggestions on how to make this less hackish? Will this approach cause problems with SSR in |
@benosman Pretty tricky approach :D But I think it would lead to potential performance problems and usage is little complicated. I'm investigating for other solutions too will make this thread updated. |
@pi0 - yes, injecting the store itself was causing some issues as expected. I've changed it now to a function that returns the $axios object, but still the usage is complicated unfortunately. Look forward to seeing your solutions on here :) |
I've continued to have a look at this, mainly as I wanted to learn a bit more how vuex works. In the end I found a way to replace the action handlers with one that contains an $axios parameter. I used a store plugin for this case, as it makes it optional. I've created a gist for the plugin here, as I wasn't sure where/how it would fit inside this nuxt module. I tried to keep the amount of code brought in from vuex to a minimum, but there is more than i'd like because the key functions are not accessible at runtime. There was talk of a similar issue about adding extra context on this vuex pull request, but work on that seems to have stopped for now. |
@benosman I think we had found a workaround it is on the way into nuxt core :) Hold on a little more :)) PS: Learning by reading source is great keep it going :) |
Cool , do you have a link to the related workaround in nuxt core? Right now, my approach seems to work so I can start building out my store modules using axios, then I will switch over when the workaround arrives. |
@pi0 can you give a high level description of what the work around will mean from a user stand point? Will this mean axios will not need to be sent with each dispatch? |
@uptownhr After this enhancement releases, we can access entire store instance inside actions and mutations. So using axios would be as easy as: actions: {
async editPost ({ commit }, { type, id }) {
// We can use this.$axios which `this` refers to current store
await this.$axios.post(`/posts/${id}`)
}
} |
@pi0 that is wonderful. I've been holding off on upgrading due to this :) |
Finally is available with [email protected] 🍸 Will be shipped with nuxt rc8. For earlier testing, the only required thing is export default {
actions: {
getProfile() {
const profile = await this.axios.$get('user/profile')
}
}
} |
Is this still not available in nuxt rc11?! |
@MRZMUH1001 Yes it's already available :) Refer to the docs for usage. |
Hi I can't find to work with nuxt-axios in my store which actions is separated in a different file actions.js Can u help me what's the problem? |
Is there no way to inject or import the configured axios module into the store instance? Having to pass it as an additional argument to the store action doesn't feel right and really limits usage.
The text was updated successfully, but these errors were encountered: