Count: {{ count }}
\nDoubled: {{ doubled }}
\n \n\n```\n\n**Key features:**\n- DevTools support\n- Hot module replacement\n- TypeScript inference\n- SSR support\n- No mutations required"}}]}Pinia — official Vue state management library.
Setup:
1npm install pinia
Store definition:
1// stores/counter.ts2import { defineStore } from "pinia"3import { ref, computed } from "vue"45export const useCounterStore = defineStore("counter", () => {6 const count = ref(0)7 const doubled = computed(() => count.value * 2)89 function increment() {10 count.value++11 }1213 async function fetchAndIncrement() {14 await api.increment()15 count.value++16 }1718 return { count, doubled, increment, fetchAndIncrement }19})
Usage in component:
1<script setup>2import { useCounterStore } from "@/stores/counter"3import { storeToRefs } from "pinia"45const counter = useCounterStore()6const { count, doubled } = storeToRefs(counter) // reactive7</script>89<template>10 <p>Count: {{ count }}</p>11 <p>Doubled: {{ doubled }}</p>12 <button @click="counter.increment()">+1</button>13</template>
Key features: