Nuxt 在方法中两次使用 useFetch/useAsyncData 会抛出错误:`nuxt 实例不可用`。

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

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();

huangapple
  • 本文由 发表于 2023年4月17日 16:29:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033123.html
匿名

发表评论

匿名网友

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

确定