【问题标题】:Make a http call with axios in the nuxtServerInit action在 nuxtServerInit 操作中使用 axios 进行 http 调用
【发布时间】:2019-01-26 14:50:11
【问题描述】:

我正在开发一个从 WordPress API 获取内容的静态网站。

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

我重新阅读了 nuxt 服务器和 nuxtServerInit 操作的文档,但我没有找到一个很好的示例来说明如何在此操作中进行轴子调用,并能够获取组件上的存储。

我找到了这个,但它不起作用..https://github.com/nuxt/nuxt.js/issues/2307

非常感谢您的帮助。

【问题讨论】:

标签: javascript node.js vue.js nuxt.js


【解决方案1】:

试试这个

存储/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
  }
}

【讨论】:

  • 点赞!这是否意味着当您在 url 中手动键入 API 的页码并执行查询时,每次都会调用 nuxtserverinit,如果您想在 servr 端分页,您将如何在外部调用它
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
  • 2019-09-29
  • 1970-01-01
相关资源
最近更新 更多