Open avxkim opened 4 years ago
I have the same issue and question.
I had the same issue and managed to execute the first action inside of a plugin, the only downside is that it is a custom action, and not the nuxtServerInit action, but for my requirements, it works fine.
Note: I am working with universal mode
import { Context } from "@nuxt/types"
import { createStore } from "../utils/store-accesor"
export default (ctx: Context, inject) => {
// Create the store (I am doing this for my own code to avoid calling getModule on each component I want to dispatch an action)
const _store = createStore(ctx.store)
// Dispatch the first action (This is where I can check if the user is logged in, etc)
ctx.store.dispatch("startup/init", ctx, { root: true })
// Inject the store
inject("appStore", _store)
}
I hope this can help you
Here is where to use nextServerInit(): https://github.com/championswimmer/vuex-module-decorators/issues/80#issuecomment-500374511
store/settingsModule.ts:
import { store } from '@/store'
@Module({
name: 'settingsModule',
stateFactory: true,
namespaced: true,
dynamic: true,
store,
})
export default class settingsModuleextends VuexModule {
// The internal operation
@Action
async obtainListData() {
// An asynchronous request
const { data } = await $GetClassification()
this.context.commit('mutationName', data)
}
}
store/index.ts:
export const store: Store<any> = new Vuex.Store({})
export const actions: ActionTree<RootState, RootState> = {
async nuxtServerInit({ commit, dispatch }, { req, route }) {
await dispatch('router/setParams', route)
}
}
Using this approach with Nuxt: https://github.com/championswimmer/vuex-module-decorators#accessing-modules-with-nuxtjs
store/index.ts:
utils/store-accessor.ts:
store/settingsModule.ts:
How do i actually call
nuxtServerInit()
? If i try inside "store/index.ts":Doesn't fire my action