如何在Nuxt 2中使用@nuxtjs/axios和@nuxtjs/composition-api:

huangapple go评论61阅读模式
英文:

How to use @nuxtjs/axios with @nuxtjs/composition-api and nuxt 2

问题

我想在Nuxt 2.17中使用https://www.npmjs.com/package/@nuxtjs/axios与插件https://composition-api.nuxtjs.org/ (@nuxtjs/composition-api)。

现在当然不能使用this.$axios。我想我可以通过某种方式注入它,但到目前为止还没有成功。

我看到了这个解决方案:https://stackoverflow.com/a/73237334/1121268
但使用Vue的getCurrentInstance似乎有点笨拙。

感谢任何有关如何在组合API中使用$axios的提示。

英文:

I want to use https://www.npmjs.com/package/@nuxtjs/axios with nuxt 2.17 and the plugin https://composition-api.nuxtjs.org/ (@nuxtjs/composition-api).

Now the usage of this.$axios does not work of course. I thought I might could inject it somehow, but was not successful so far.

I see this solution: https://stackoverflow.com/a/73237334/1121268
but it seems to be a bit clumsy to use getCurrentInstance of Vue.

Thanks for any hint how to use the $axios with the composition api.

答案1

得分: 1

你可以使用 Nuxt Composition-API 的 useContext 来实现这一点。我已经添加了一个可用的简单代码和下面的链接。

要使用组合 API useContext 访问 Nuxt 的上下文,如@nuxtjs/composition-api文档所述,您需要导入 useContext

例如:import { useContext } from '@nuxtjs/composition-api'

这样,您就可以访问所有 Nuxt 上下文,而在这种情况下,您还可以访问 $axios 模块。

import { ref, defineComponent, useContext } from "@nuxtjs/composition-api";

export default defineComponent({
  setup() {
    const todo = ref([]);
    const fetching = ref(false);
    const { $axios } = useContext();

    const fetchPosts = async () => {
      try {
        fetching.value = true;
        const response = await $axios.get(
          "https://jsonplaceholder.typicode.com/todos/1"
        );
        todo.value = response.data;
      } catch (error) {
        // 在此处理错误
        console.error("Error fetching todo:", error);
      } finally {
        fetching.value = false;
      }
    };

    return {
      todo,
      fetchPosts,
      fetching,
    };
  },
});
<div>
  <h1>TODOs</h1>
  <button @click="fetchPosts">Fetch Tasks</button>

  <p v-if="fetching">Fetching...</p>
  <pre v-else>
    {{ JSON.stringify(todo, null, 3) }}
  </pre>
</div>

这是代码沙盒的链接 - https://codesandbox.io/s/nuxt-2-composition-api-and-axios-integration-pmzxq8

英文:

You could do that using the Nuxt Composition-API's useContext. I've added a working simple code and link below.

To access Nuxt's context using the composition API useContext as explained on @nuxtjs/composition-api doc,
you need to import the useContext.

e.g. import { useContext } from &#39;@nuxtjs/composition-api&#39;

This way you can access all Nuxt context and in this case, you can access $axios module too.

<!-- language: lang-js -->

import { ref, defineComponent, useContext } from &quot;@nuxtjs/composition-api&quot;;

export default defineComponent({
  setup() {
    const todo = ref([]);
    const fetching = ref(false);
    const { $axios } = useContext();

    const fetchPosts = async () =&gt; {
      try {
        fetching.value = true;
        const response = await $axios.get(
          &quot;https://jsonplaceholder.typicode.com/todos/1&quot;
        );
        todo.value = response.data;
      } catch (error) {
        // Handle the error here
        console.error(&quot;Error fetching todo:&quot;, error);
      } finally {
        fetching.value = false;
      }
    };

    return {
      todo,
      fetchPosts,
      fetching,
    };
  },
});

<!-- language: lang-html -->

  &lt;div&gt;
    &lt;h1&gt;TODOs&lt;/h1&gt;
    &lt;button @click=&quot;fetchPosts&quot;&gt;Fecth Tasks&lt;/button&gt;

    &lt;p v-if=&quot;fetching&quot;&gt;Fetching...&lt;/p&gt;
    &lt;pre v-else&gt;
      {{ JSON.stringify(todo, null, 3) }}
    &lt;/pre&gt;
  &lt;/div&gt;

<!-- end snippet -->`

Here is the link to the codesandbox - https://codesandbox.io/s/nuxt-2-composition-api-and-axios-integration-pmzxq8

huangapple
  • 本文由 发表于 2023年7月13日 23:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定