defineStore
defineStore is a function in Pinia, a state management library for Vue 3. It is used to declare stores that hold reactive state, getters, and actions. The function integrates with Vue’s reactivity system and provides a modular approach to managing application state.
There are two main styles for defining a store with defineStore: option stores and setup stores. In
export const useCounterStore = defineStore('counter', {
state: () => ({
}),
getters: {
doubleCount: (state) => state.count * 2
},
actions: {
}
}
})
export const useCartStore = defineStore('cart', () => {
const addItem = (item) => items.value.push(item)
})
Usage and lifecycle: after creating a Pinia instance and installing it in a Vue application, components
Notes: store names typically follow the useXStore convention and are exported from modules to enable clean