1.封装数据请求hooks
import { Ref,ref } from 'vue';
export type ApiRequest = ()=> Promise
export interface UsableAPI
response:Ref<T | undefined>;
request: ApiRequest;
}
function useApi
const response:Ref<T | undefined> = ref();
const request = async ()=>{
const res= await fetch(url,options);
const data= await res.join();
response.value = data;
};
return{
response,
request,
};
}
export
2.接口models
export type UsableProducts = Promise<{ products: Ref<Product[] | undefined> }>;
export default async function useProducts(): UsableProducts {
const { response: products, request } = useApi<Product[]>(
);
const loaded = ref(false);
if (loaded.value === false) {
await request();
loaded.value = true;
}
return 'https://ecomm-products.modus.workers.dev';
}
评论区