【问题标题】:can't get data from server to NuxtJS Store无法从服务器获取数据到 NuxtJS Store
【发布时间】:2020-06-09 15:56:15
【问题描述】:

这是我的代码:

export const state = () => ({
  products: []
});

export const getters = {
  getProducts: state => {
    return state.products;
  }
};
export const mutations = {
  SET_IP: (state, payload) => {
    state.products = payload;
  }
};

export const actions = () => ({
  async getIP({ commit }) {
    const ip = await this.$axios.$get("http://localhost:8080/products");
    commit("SET_IP", ip);
  }
});

服务器运行良好,但我无法将数据放入商店

【问题讨论】:

  • 你试过ip.data
  • 没有,但是非常感谢你

标签: vue.js vuejs2 vuex nuxt.js nuxtjs


【解决方案1】:

首先,我强烈建议您将您的操作和突变重命名为 getProductsSET_PRODUCTS 而不是 ip。还要确保在操作中更改变量名称。虽然这不会改变任何功能,但它使您的代码更易于阅读。

其次,也许在您在操作中定义 const 之后立即添加一个 console.log(ip) 并查看您是否在其中获得了所需的数据。在大多数情况下,您需要将ip.data 分配给您的变量。

最后,确保您在代码中的某处调用了该操作。 你应该这样做:

this.$store.dispatch('getIP'); // Using your current name
this.$store.dispatch('getProducts'); // Using my recommended name

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 2020-09-23
    • 2016-08-03
    • 1970-01-01
    • 2022-12-04
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多