nuxt3.0获取数据$fetch和useFetch的区别

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Nuxt.js 3 is an upcoming version of the popular Vue.js-based framework that provides a lot of powerful features. One of the new features introduced in Nuxt.js 3 is the composables API, which allows developers to create reusable logic that can be used across multiple components. One of the composables available in Nuxt.js 3 is `$fetch`. This composable provides a way to fetch data from an API endpoint using the Fetch API. To use `$fetch`, you need to import it from `@nuxtjs/composition-api` and then call it in your component. Here's an example: ``` <template> <div> <h1>Posts</h1> <ul> <li v-for="post in posts" :key="post.id">{{ post.title }}</li> </ul> </div> </template> <script> import { ref } from '@nuxtjs/composition-api'; export default { setup() { const posts = ref([]); const fetchPosts = async () => { const response = await $fetch('https://jsonplaceholder.typicode.com/posts'); posts.value = await response.json(); }; fetchPosts(); return { posts, }; }, }; </script> ``` In this example, we import `ref` from `@nuxtjs/composition-api` and use it to define a reactive `posts` variable. We then define a function called `fetchPosts` that calls `$fetch` to fetch data from the API endpoint and updates the `posts` variable. Finally, we call `fetchPosts` in the `setup` function and return `posts` as a property of the component. Note that `$fetch` is only available in components that use the `setup` function, which is a new syntax introduced in Nuxt.js 3. Also, `$fetch` only works with server-side rendering (SSR) and cannot be used in client-only components.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸥总

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值