创建新类别FakeStoreApi Vuejs Nuxt3

问题描述 投票:0回答:1

我想知道如何使用 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,
  };
}

javascript vue.js fetch-api nuxt3
1个回答
0
投票

从文档中您需要将

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))
© www.soinside.com 2019 - 2024. All rights reserved.