[Vuejs]-Using the NuxtJS Strapi Module in Vuex

0👍

there is this nuxtServerInit action that gets executed on the server side after you open / refresh page.

You can only do it in index.js file

This is how your index.js could look like:

//index.js
export const state = () => ({
   someState: false
})


export const mutations = {
  SOME_MUTATION(state, payload) {
    ...
  }
}

export const actions = {
  SOME_ACTION({ state, commit }, payload) {
     ...
  },
  nuxtServerInit({ commit }, { $strapi }){
     //do here something on the server side...
  }
}

The documentation says if you added the strapi module to your project, you should be able to access it from the context. The context is the second argument from nuxtServerInit

Thats it. This is how a store could look like, nothing fancy.

You could dispatch / commit something from any component with

this.$store.commit("SOME_MUTATION")

They say its global available with this.$strapi

This module globally injects $strapi instance, meaning that you can access it anywhere using this.$strapi. For plugins, asyncData, fetch, nuxtServerInit and Middleware, you can access it from context.$strapi.

Leave a comment