Composable with onMounted
Fetch mock JSON on mount and store it in a ref.
import { onMounted, ref } from "vue";
const API_URL = "https://free-api-backend.vercel.app/products?count=12";
export function useProducts() {
const products = ref([]);
onMounted(async () => {
const res = await fetch(API_URL);
const json = await res.json();
products.value = json.data ?? [];
});
return { products };
}VueUse useFetch
Point VueUse at any ApiGenerator URL for reactive loading states.
import { useFetch } from "@vueuse/core";
export function useMockUsers() {
return useFetch("https://free-api-backend.vercel.app/users?count=10&fields=id,firstName,email").json();
}Frequently asked questions
How do I use a mock API in Vue 3?
Fetch ApiGenerator endpoints inside onMounted or a composable. Responses return JSON with a data array you can assign to refs.
Does this work with Nuxt?
Yes. Call the same REST URLs from useFetch, $fetch, or server routes during prototyping.
Can I keep list and detail data in sync?
Pass the same seed query parameter on list and detail requests so IDs and fields stay stable while you build navigation.