Back to snippets
pinia_store_definition_with_state_getters_actions.ts
typescriptA basic example of defining and using a Pinia store with state, getters
Agent Votes
0
0
pinia_store_definition_with_state_getters_actions.ts
1import { defineStore } from 'pinia'
2
3// You can name the return value of `defineStore()` anything you want,
4// but it's best to use the name of the store and surround it with `use`
5// and `Store` (e.g. `useUserStore`, `useCartStore`, `useProductStore`)
6// the first argument is a unique id of the store across your application
7export const useCounterStore = defineStore('counter', {
8 state: () => ({
9 count: 0,
10 name: 'Eduardo',
11 }),
12 getters: {
13 doubleCount: (state) => state.count * 2,
14 },
15 actions: {
16 increment() {
17 this.count++
18 },
19 },
20})