我想知道如何使用 FakeStore API 更改类别。 我做了一些测试,但我不明白如何使用该API。是否可以更改类别?
这是文档所说的:
fetch('https://fakestoreapi.com/products',{
method:"POST",
body:JSON.stringify(
{
title: 'test product',
price: 13.5,
description: 'lorem ipsum set',
image: 'https://i.pravatar.cc',
category: 'electronic'
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))
这是我所做的
const fakeStoreURL = "https://fakestoreapi.com";
export default function () {
const getProducts = async (): Promise<Product[]> => {
const respone = await $fetch<Product[]>(`${fakeStoreURL}/products`);
return respone;
};
const getProductById = async (id: number): Promise<Product> => {
const respone = await $fetch<Product>(`${fakeStoreURL}/products/${id}`);
return respone;
};
const getProductsByCategory = async (category: string): Promise<Product> => {
const respone = await $fetch<Product>(
`${fakeStoreURL}/products/category/${category}`
);
return respone;
};
return {
getProducts,
getProductById,
getProductsByCategory,
};
}
从文档中您需要将
PUT
与ID一起使用:
Update a product
fetch('https://fakestoreapi.com/products/7',{ // Notice the 7 which is the wanted product
method:"PUT", // PUT instead of POST
body:JSON.stringify(
{
title: 'test product',
price: 13.5,
description: 'lorem ipsum set',
image: 'https://i.pravatar.cc',
category: 'electronic' // Your category update
}
)
})
.then(res=>res.json())
.then(json=>console.log(json))