[Vuejs]-Using nuxt 3 useFetch in layouts

0👍

You can use a condition to check the route name using useRoute and then make an API call. Since pages/[category]/index.vue and pages/[id].vue are two different routes with their own unique route names.

<script setup>
const route = useRoute();
const myCategories = ref([]);

//you can check exact route details from vue devtools extension's routes section.
if(route.name == 'pages-category'){
   const { data:allCategories } = await useFetch('/api/get-categories/');
   myCategories.value = allCategories;
}
</script>

Leave a comment