英文:
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 '@nuxtjs/composition-api'
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 "@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) {
// Handle the error here
console.error("Error fetching todo:", error);
} finally {
fetching.value = false;
}
};
return {
todo,
fetchPosts,
fetching,
};
},
});
<!-- language: lang-html -->
<div>
<h1>TODOs</h1>
<button @click="fetchPosts">Fecth Tasks</button>
<p v-if="fetching">Fetching...</p>
<pre v-else>
{{ JSON.stringify(todo, null, 3) }}
</pre>
</div>
<!-- end snippet -->`
Here is the link to the codesandbox - https://codesandbox.io/s/nuxt-2-composition-api-and-axios-integration-pmzxq8
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论