Axel
Axel

Reputation: 5131

Vue Nuxt nuxtServerInit doesn't load the data

In the Vuex Store, I define nuxtServerInit() which doesn't initialize at all!

import Vuex from 'vuex'
import mod1 from './modules/mod1'
import mod2 from './modules/mod2'

const store = () => {
    return new Vuex.Store({
        actions: {
            nuxtServerInit() {
                setTimeout(() => console.log('Hello'), 10000)   
            }
        },
        modules: {
           mod1,
           mod2
        }
    })
}

export default store

Am I doing something wrong? Please help!

Upvotes: 1

Views: 2672

Answers (2)

Nicola De Lazzari
Nicola De Lazzari

Reputation: 204

Are you sure that you are calling NuxtServerInit from store/index.js and you are not in a module mode?

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.

https://nuxtjs.org/guide/vuex-store/

Upvotes: 1

Mahamudul Hasan
Mahamudul Hasan

Reputation: 2823

You can try like below example

actions: {
  nuxtServerInit ({ commit }, { req }) {
    if (req.session.user) {
      commit('user', req.session.user)
    }
  }
}

more details here

Upvotes: 0

Related Questions