I'm working on a static website fetching content from the WordPress API.
On the menu of the website, I want the content to be save on a nuxt store, and available on the nav ponent.
I reed the doc of the nuxt server and the nuxtServerInit action, but I didn't find a nice example of how to make a axion call inside this action, and be able to fetch the store on the ponent.
I find this, but it's not working .. .js/issues/2307
Thanks a lot for your help.
I'm working on a static website fetching content from the WordPress API.
On the menu of the website, I want the content to be save on a nuxt store, and available on the nav ponent.
I reed the doc of the nuxt server and the nuxtServerInit action, but I didn't find a nice example of how to make a axion call inside this action, and be able to fetch the store on the ponent.
I find this, but it's not working .. https://github./nuxt/nuxt.js/issues/2307
Thanks a lot for your help.
Share Improve this question asked Aug 20, 2018 at 13:00 Kévin FuretKévin Furet 3451 gold badge8 silver badges21 bronze badges 1- You can use property asyncData for load content. Link nuxtjs/guide/async-data – Николай Commented Aug 20, 2018 at 14:05
1 Answer
Reset to default 9Try this
store/index.js
export const state = () => ({
data: null
})
export const actions = {
// nuxtServerInit is called by Nuxt.js before server-rendering every page
async nuxtServerInit({ mit, dispatch }) {
await dispatch('storeDispatchFunc')
},
// axios...
async storeDispatchFunc({ mit }) {
const { data } = await this.$axios.get('/api/wp....')
mit('SET_DATA', data)
},
}
export const mutations = {
SET_DATA(state, theData) {
state.data = theData
}
}