nuxt.js客户端获取数据

问题描述

我有一个nuxt.js应用程序,其中使用了fetch()挂钩从API提取数据,如下所示:

async fetch() {
    await this.$store.dispatch('articles/getArticlesDetails',this.$route.params.id)
}

并且我想在客户端使用API​​返回的数据,并尝试将其装入安装,并且在装入中未定义。

还有没有其他有效的方法来获取和使用数据,以便进一步在客户端进行API调用。

解决方法

我假设您的Vuex Action对State数据进行了突变:

store.js

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

然后,您应该能够从_id.vue中检索更新后的状态:

export default {
    computed: {
      apiData() {
        return this.$store.state.dataFromAPI
      }
    }
  }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...