// stores/users.js
import { defineStore } from 'pinia';
import { ref } from 'vue';
export const useUsersStore = defineStore('users', () => {
const users = ref([]);
const status = ref('idle'); // idle | loading | error | success
async function fetchUsers() {
status.value = 'loading';
try {
const res = await fetch('https://jsonplaceholder.typicode.com/users');
if (!res.ok) throw new Error('Request failed');
users.value = await res.json();
status.value = 'success';
} catch (e) {
status.value = 'error';
}
}
return { users, status, fetchUsers };
});
Loading...
Something went wrong.