Vue · Junior · Frage 49 von 75
Wie definiert und nutzt man einen Pinia-Store?
export const useCounterStore = defineStore("counter", () => {
const count = ref(0);
const double = computed(() => count.value * 2);
function increment() { count.value++; }
return { count, double, increment };
});
// in a component: const counter = useCounterStore();- 1Mit einem aus einer Datei exportierten
reactive-Objekt, da Pinia keine API hat - 2Mit
defineStore, mit Refs, computed und Funktionen - 3Mit
new Store()inmain.jsundthis.$storein Komponenten
Antwort zeigen
Richtige Antwort: Mit defineStore, mit Refs, computed und Funktionen
Erklärung
Das ist ein Setup Store; es gibt auch Option Stores mit state, getters und actions. Der Store wird bei der ersten Nutzung erzeugt und von Komponenten geteilt.