Javascript 在nuxtServerInit操作中使用axios进行http调用

Javascript 在nuxtServerInit操作中使用axios进行http调用,javascript,node.js,vue.js,nuxt.js,Javascript,Node.js,Vue.js,Nuxt.js,我正在开发一个静态网站,从WordPressAPI获取内容 在网站的菜单上,我希望内容保存在nuxt商店中,并在nav组件上可用 我查看了nuxt服务器的文档和nuxtServerInit操作,但是我没有找到一个很好的示例,说明如何在此操作中进行axion调用,并能够获取组件上的存储 我发现了这个,但它不起作用 非常感谢您的帮助。试试这个 store/index.js export const state = () => ({ data: null }) export const a

我正在开发一个静态网站,从WordPressAPI获取内容

在网站的菜单上,我希望内容保存在nuxt商店中,并在nav组件上可用

我查看了nuxt服务器的文档和nuxtServerInit操作,但是我没有找到一个很好的示例,说明如何在此操作中进行axion调用,并能够获取组件上的存储

我发现了这个,但它不起作用

非常感谢您的帮助。

试试这个

store/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
  }
}
试试这个

store/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
  }
}

可以使用属性asyncData加载内容。链接您可以使用属性asyncData加载内容。链接向上投票!这是否意味着,当您在url中手动键入API的页码并执行每次调用nuxtserverinit的查询时,如果您希望对servr SideUp进行分页,您将如何在外部调用它!这是否意味着,当您在url中手动键入API的页码并执行每次调用nuxtserverinit的查询时,如果您希望对servr端进行分页,您将如何在外部调用它