英文:
Nuxt useFetch/useAsyncData twice in method throws error: `nuxt instance not available`
问题
I encounter the classical nuxt instance not available error when server side rendering a nuxt page containing the following logic:
<script setup>
const getImageOne = async () => {
  return await useFetch('https://dog.ceo/api/breeds/image/random');
};
const getImageTwo = async () => {
    return await useFetch('https://dog.ceo/api/breeds/image/random');
};
const getImages = async () => {
  return [await getImageOne(), await getImageTwo()];
};
const responses = await getImages();
const dogOne = responses?.[0]?.data?.value;
const dogTwo = responses?.[1]?.data?.value;
</script>
<template>
  <div>
    <p>Dog 1: {{ dogOne }}</p>
    <p>Dog 2: {{ dogTwo }}</p>
  </div>
</template>
The issue seems to be that useFetch is called twice in one method call.
英文:
I encounter the classical nuxt instance not available error when server side rendering a nuxt page containing the following logic:
<script setup>
const getImageOne = async () => {
  return await useFetch('https://dog.ceo/api/breeds/image/random');
};
const getImageTwo = async () => {
    return await useFetch('https://dog.ceo/api/breeds/image/random');
};
const getImages = async () => {
  return [await getImageOne(), await getImageTwo()];
};
const responses = await getImages();
const dogOne = responses?.[0]?.data?.value;
const dogTwo = responses?.[1]?.data?.value;
</script>
<template>
  <div>
    <p>Dog 1: {{ dogOne }}</p>
    <p>Dog 2: {{ dogTwo }}</p>
  </div>
</template>
The issue seems to be that useFetch is called twice in one method call.
答案1
得分: 1
The issue with composable usage raising nuxt instance not available errors is already well known, see issue #14723 for reference.
A solution in this case would be to use the callWithNuxt method and provide the nuxt instance manually like this:
<script setup>
import { callWithNuxt } from 'nuxt/app';
const getImageOne = async () => {
  return await useFetch('https://dog.ceo/api/breeds/image/random');
};
const getImageTwo = async (app) => {
  👇
  return await callWithNuxt(
    app,
    async () => await useFetch('https://dog.ceo/api/breeds/image/random')
  );
};
const getImages = async () => {
  👇
  const app = useNuxtApp();
  return [await getImageOne(), await getImageTwo(app)];
};
const responses = await getImages();
⋮
英文:
The issue with composable usage raising nuxt instance not available errors is already well known, see issue #14723 for reference.
A solution in this case would be to use the callWithNuxt method and provide the nuxt instance manually like this:
<script setup>
import { callWithNuxt } from 'nuxt/app';
const getImageOne = async () => {
  return await useFetch('https://dog.ceo/api/breeds/image/random');
};
const getImageTwo = async (app) => {
  👇
  return await callWithNuxt(
    app,
    async () => await useFetch('https://dog.ceo/api/breeds/image/random')
  );
};
const getImages = async () => {
  👇
  const app = useNuxtApp();
  return [await getImageOne(), await getImageTwo(app)];
};
const responses = await getImages();
⋮
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论