我正在开发一个静态网站,从 WordPress API 获取内容。
在网站的菜单上,我希望内容保存在 nuxt 存储上,并在导航组件上可用。
我查阅了 nuxt 服务器和 nuxtServerInit 操作的文档,但我没有找到一个很好的示例来说明如何在此操作中进行 axion 调用,并能够获取组件上的存储。
I find this, but it's not working .. https://github.com/nuxt/nuxt.js/issues/2307
非常感谢您的帮助。
最佳答案
试试这个
商店/index.js
export const state = () => ({
data: null
})
export const actions = {
// nuxtServerInit is called by Nuxt.js before server-rendering every page
async nuxtServerInit({ commit, dispatch }) {
await dispatch('storeDispatchFunc')
},
// axios...
async storeDispatchFunc({ commit }) {
const { data } = await this.$axios.get('/api/wp....')
commit('SET_DATA', data)
},
}
export const mutations = {
SET_DATA(state, theData) {
state.data = theData
}
}
关于javascript - 在 nuxtServerInit 操作中使用 axios 进行 http 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51931334/