首页 > 解决方案 > 在 nuxtServerInit() 中使用 Axios

问题描述

我需要让远程数据显示在每个页面中。

此调用在以下位置执行store/index.js

export const state = () => ({
  contact: {
    hello: "World"
  }
});

export const actions = {
  async nuxtServerInit({ commit, state }) {
    const { contactData } = await this.$axios.get("/contact");
    commit("SET_CONTACT", contactData);
  }
};

export const mutations = {
  SET_CONTACT(state, contactData) {
    state.contact = contactData;
  }
};

问题是store中的contactturns的值undefined,而预期的内容是通过axios检索的(检索到的内容显示在SSR控制台...)

我在这里想念什么?

标签: vue.jsaxiosvuexnuxt.jsnuxtserverinit

解决方案


推荐阅读