简体   繁体   English

如何在 Nuxt3 中使用 @nuxtjs/axios 模块?

[英]How to use the @nuxtjs/axios module with Nuxt3?

I have this code to get API data from https://fakestoreapi.com/products/我有这段代码可以从https://fakestoreapi.com/products/获取 API 数据

<template>
<div>


</div>
</template>


  <script>  
  definePageMeta({
    layout: "products"
  })

export default {
  data () {
    return {
      data: '',
    }
  },
  async fetch() {
    const res = await this.$axios.get('https://fakestoreapi.com/products/')
    console.log(res.data)
  },
}
</script>

I have installed axios and in nuxt.config.ts I have:我已经安装了 axios 并且在nuxt.config.ts中我有:

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({

    app: {
        head: {
            title: 'Nuxt',
            meta: [
                { name: 'description', content: 'Everything about - Nuxt-3'}
            ],
            link: [
                {rel: 'stylesheet', href: 'https://fonts.googleapis.com/icon?family=Material+Icons' }
            ]
        }
    },
    runtimeConfig: {
        currencyKey: process.env.CURRENCY_API_KEY
    },
    modules: [
        "@nuxtjs/tailwindcss",
    ],
    buildModules: [
        "@nuxtjs/axios"
    ],
    axios: {
      baseURL: '/',
    }
})

I have the following in my console我的控制台中有以下内容

is an experimental feature and its API will likely change.是一项实验性功能,其 API 可能会更改。

I am not getting API data in the console.我没有在控制台中获得 API 数据。

As told on this page , we don't use the @nuxtjs/axios module anymore with Nuxt3 but rather ohmyfetch , which is now baked-in directly in the core of the framework through $axios as writted here .本页所述,我们不再将@nuxtjs/axios模块与 Nuxt3 一起使用,而是使用ohmyfetch ,它现在通过 此处编写的$axios直接嵌入到框架的核心中。

Hence, your config file should look like this因此,您的配置文件应如下所示

export default defineNuxtConfig({
  app: {
    head: {
      title: 'Nuxt Dojo',
      meta: [
        { name: 'description', content: 'Everything about - Nuxt-3' }
      ],
      link: [
        { rel: 'stylesheet', href: 'https://fonts.googleapis.com/icon?family=Material+Icons' }
      ]
    }
  },
  runtimeConfig: {
    currencyKey: process.env.CURRENCY_API_KEY
  },
  modules: [
    "@nuxtjs/tailwindcss"
  ],
})

And the given /pages/products/index.vue can be like that给定的/pages/products/index.vue可以是这样的

<template>
  <div>
    <p v-for="user in users" :key="user.id">ID: {{ user.id }} 👉 {{ user.name }}</p>
  </div>
</template>


<script>
definePageMeta({
  layout: "products"
})

export default {
  data () {
    return {
      users: '',
    }
  },
  async mounted() {
    this.users = await $fetch('https://jsonplaceholder.typicode.com/users')
  },
}
</script>

This is how it looks at the end (with a successful HTTP request in the.network tab)这就是它最后的样子(在 .network 选项卡中成功请求 HTTP)

在此处输入图像描述


As a confirmation, we can see that the module is indeed not supported (and will not be) by Nuxt3 on the modules page .作为确认,我们可以在模块页面上看到 Nuxt3 确实不支持(也不会)支持该模块。

The Suspense error is detailed in the official documentation Suspense错误在官方文档中有详细说明

<Suspense> is an experimental feature. <Suspense>是一项实验性功能。 It is not guaranteed to reach stable status and the API may change before it does.不能保证达到稳定状态,API 可能会在此之前发生变化。

It may seem scary but you can totally use the API as per se and since it's a warning and not an error, it's totally fine!它可能看起来很可怕,但您完全可以按照本身使用 API,因为它是警告而不是错误,所以完全没问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM