为什么在商店中不工作?

发布于 2025-01-29 03:26:13 字数 1049 浏览 2 评论 0原文

对于我的项目,我正在使用nuxt。在商店的操作中,我有这个代码:

async nuxtServerInit({ commit }) {
  this.dispatch('fetchAllClients')
},
async fetchAllClients({ commit, state }) {
  console.log('fetch clients')
  await fetch('http://localhost:1337/api/clients', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  })
    .then((res) => res.json())
    .then((res) => commit('storeClients', res))
},

因此,在nuxt init上,我希望运行一些获取,因此可以更新某些状态(这是在de storeclients()中完成的。在提取中)。如果我在nuxtserverinit()内复制fetchallclients()方法,那么一切都很好。

async nuxtServerInit({ commit }) {
  await fetch('http://localhost:1337/api/clients', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  })
    .then((res) => res.json())
    .then((res) => commit('storeClients', res))
},

但是,当我调用上面代码中所示的单独方法时,获取将无法正常工作。显示了日志,因此该方法被正确调用。但是以某种方式提取什么都没做。我在做什么错?

For my project I'm using Nuxt. In the store's action I have this piece of code:

async nuxtServerInit({ commit }) {
  this.dispatch('fetchAllClients')
},
async fetchAllClients({ commit, state }) {
  console.log('fetch clients')
  await fetch('http://localhost:1337/api/clients', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  })
    .then((res) => res.json())
    .then((res) => commit('storeClients', res))
},

So, on Nuxt init I would like some fetches to be run, so it can update some states (this is done in de storeClients() which is committed in the fetch). If I copy the fetchAllClients() method inside the nuxtServerInit() then everything works just fine.

async nuxtServerInit({ commit }) {
  await fetch('http://localhost:1337/api/clients', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  })
    .then((res) => res.json())
    .then((res) => commit('storeClients', res))
},

But when I call a separate method as shown in the code above, then fetch won't work. The log is shown, so the method is called properly. But somehow the fetch doesn't do anything. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2025-02-05 03:26:13

你应该有这个

async nuxtServerInit({ dispatch }) {
  await dispatch('fetchAllClients')
}

You should have this instead

async nuxtServerInit({ dispatch }) {
  await dispatch('fetchAllClients')
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文