渲染后按需请求 Nuxt 3 HTTP 请求:客户端获取

Nuxt 3 具有那些惊人的数据获取功能(例如:useFetch),但我发现我需要在渲染时间之后发出请求(例如:从按钮调用并发送搜索词)。

据我所知,useFetch 不在客户端工作,这就是我想要做的

<template>

<button @click="goSearch()">Search</button>

</template>

setup() {
        const goSearch = async () => {
            const { data } = await useFetch('search', () => $fetch('/api/search'));
            console.log(data.value);
        };

        return { goSearch };
    },
}

nuxt3 是否提供内置功能来按需发出 http 请求(客户端官方 http axios 之类的)?

stack overflow Nuxt 3 HTTP request on demand after rendering: a client-side fetch
原文答案
author avatar

接受的答案

$fetch 应该可以工作。问题是一个小错误,现已修复。如果您遇到此错误,只需升级 nuxt/ohmyfetch lib

npx nuxi upgrade --force

更多信息: https://github.com/nuxt/framework/issues/2502#issuecomment-999783226


答案:

作者头像

useFetch is the same as using $fetch, here why not simply use fetch already in your code ?

const { data } = await $fetch('/api/search' );

i think you code is simply no just youcan use useFetch even on client side but like that :

const { data } = await useFetch('/api/search')