samtgarson / nuxt-env

Inject env vars for your Nuxt app at runtime
170 stars 15 forks source link

VUEX env vars #15

Closed robsontenorio closed 5 years ago

robsontenorio commented 5 years ago

I am trying to use an env var as initial state on VUEX. Both does not work, because this does not exists. Any tips?

export const state = () => ({
  tv_mode: this.$env.TV_MODE
}
const initialMode = this.$env.TV_MODE

export const state = () => ({
  tv_mode: initialMode
}

ERROR

 ERROR  Cannot read property '$env' of undefined                                                                                                               22:48:42

  at o (webpack/bootstrap:2:0)
  at new Module (node_modules/vuex/dist/vuex.common.js:102:50)
  at ModuleCollection.register (node_modules/vuex/dist/vuex.common.js:191:19)
  at new ModuleCollection (node_modules/vuex/dist/vuex.common.js:162:8)
  at new Store (node_modules/vuex/dist/vuex.common.js:313:19)
  at Function (webpack/bootstrap:2:0)
  at Se (webpack/bootstrap:2:0)
samtgarson commented 5 years ago

this inside the store is the store instance itself, which has not been created at the time of that function being run.

You have two options:

robsontenorio commented 5 years ago

Just used nuxtInitServer() and called mutation for set initial state.