I just started learning nuxtjs (have some experience in vue.js) and I stuck into the following problem:
I have a simple todo-[id].vue
page. It should get an todo from the API and render on the page:
<script setup>
import Section from '~~/components/UI/containers/section.vue';
import ButtonLink from '~~/components/UI/buttons/button-link.vue';
const route = useRoute()
const todoId = parseInt(route.params.id)
const { data: todo } = await useFetch(`https://jsonplaceholder.typicode.com/todos/${todoId}`)
</script>
<template>
<Section>
<div class="mt-4 flex space-x-4">
<ButtonLink :to="{name: 'todo-id', params: {id: todoId - 1}}" type="secondary" v-show="todoId !== 1">previous</ButtonLink>
<ButtonLink :to="{name: 'todo-id', params: {id: todoId + 1}}">next</ButtonLink>
</div>
<div class="mt-6">
<p class="text-lg" :class="[todo.completed ? 'text-green-600' : 'text-red-600']">{{ todo.title }} (#{{ todo.id }})</p>
</div>
</Section>
</template>
It works fine on the server side, but when I'm clicking on the next
button it does not call the API to get next item, page's url is changing. What should I do to call the API on the client side? As I remember it works fine in vue.js
button-link
? Also, where is the code of your snippet located? Intodo-[id].vue
too? What do you see in your console/network tab? – Toler