Код IT
← Каталог

Vue — Router, Pinia и Vite — Pinia — state

Фрагмент из «Vue — Router, Pinia и Vite»: Pinia — state.

javascript javascriptencyclopedia3-ecosystem-2-frontend-frameworks-2-vue-284 embed URL статья в энциклопедии
JavaScript main.js
// stores/notes.js
import { defineStore } from 'pinia';

export const useNotesStore = defineStore('notes', {
  state: () => ({ items: [], loading: false }),
  actions: {
    async fetchAll() {
      this.loading = true;
      const res = await fetch('http://127.0.0.1:3000/notes');
      this.items = await res.json();
      this.loading = false;
    },
  },
});
// stores/notes.js
import { defineStore } from 'pinia';

export const useNotesStore = defineStore('notes', {
  state: () => ({ items: [], loading: false }),
  actions: {
    async fetchAll() {
      this.loading = true;
      const res = await fetch('http://127.0.0.1:3000/notes');
      this.items = await res.json();
      this.loading = false;
    },
  },
});