nuxt3使用 ofetch 的$fetch来发请求,当然你也可以继续用你的axios或者别的,如果请求是在服务端完成的,nuxt推荐使用内置方法useFetch来发请求,所以nuxt3发请求,是推荐$fetch和useFetch组合使用的。
简单来说:服务端发请求使用useFetch,客户端发请求使用$fetch
// 服务端发请求
useFetch(`${apiBaseUrl}${url}`, {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;'
},
body: {},
}).then((res) => {
console.log(res.data.value)
}).catch((err) => {
console.log(err)
})
// 客户端发请求
$fetch(`${apiBaseUrl}${url}`, {
method:'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;'
},
body: {},
}).then((res: any) => {
console.log(res)
}).catch((err: any) => {
console.log(err)
})


595

被折叠的 条评论
为什么被折叠?



