Vuex-module-decorators: Nuxt Context

Created on 10 Sep 2019  路  5Comments  路  Source: championswimmer/vuex-module-decorators

Hi! I would like to use this library together with Nuxt. I tried, but the only issue I'm having is that this does not include the Nuxt context. I specifically need it so i can use the $axios instance from Nuxt within the Vuex module.

Is it possible to inject or use the Nuxt context into the Vuex module?

Most helpful comment

It would be convenient, also interested in this issue. @championswimmer Any plans?

All 5 comments

It would be convenient, also interested in this issue. @championswimmer Any plans?

I'm fairly sure this is what nuxtServerInit is for (only works in store/index.ts or store/index.js):

export const actions: Actions<RootState, RootState> = {
  async nuxtServerInit({ dispatch, commit }, { req, res, app: { $axios } }) {
    // access $axios here
  }
}

I'm fairly sure this is what nuxtServerInit is for (only works in store/index.ts or store/index.js):

export const actions: Actions<RootState, RootState> = {
  async nuxtServerInit({ dispatch, commit }, { req, res, app: { $axios } }) {
    // access $axios here
  }
}

Do you have an example of using this with the @Action decorater? I'm trying to access $fireStore in my nuxt app store. I'm assuming i'll be able to use it the same way $axios can with your suggested solution

@mcking49 my understanding is that's not possible since nuxtServerInit only works in store/index.ts (see docs):

If you are using the Modules mode of the Vuex store, only the primary module (in store/index.js) will receive this action. You'll need to chain your module actions from there.

Yes, the context passed to nuxtServerInit (which I'm destructuring above) should contain $fireStore, but note that NSI is only called server-side.

realworld-vue-typescript has an example of using an API from within the store. Perhaps that will help you? Essentially it doesn't access $axios from the context, it exposes an API in a separate file and then imports that into the store modules that need it. Maybe you could do that with your $fireStore instance?

I have documented some approaches to find out the optimum approach in this ticket: https://github.com/sniperadmin/qms-nuxt-ts/issues/17

Was this page helpful?
0 / 5 - 0 ratings