Skip to content

Fetch data from an API using Pinia in Nuxt 3 #2654

Discussion options

You must be logged in to vote

Hello,

This is how you should use it inside your component.

NOTE: I made a small change in the store that should not affect the functionality

// store
import { defineStore } from 'pinia';

export const useTodosStore = define store('todos', {
    state: () => ({
        todos: []
    }),
    getters: {
        getTodos: (state) => state.todos
    },
    actions: {
        async fetchTodos() {
            try {
                const response = await fetch('https://dummyjson.com/todos');
                const data = await response.json();
                this.todos = data.todos;
            } catch (error) {
                console.error('Error fetching todos:', error);
            }
       …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@adamboother
Comment options

Answer selected by adamboother
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants