pinia , cp |
import { createPinia } from 'pinia'; const pinia = createPinia(); app.use(pinia); |
Create a pinia instance |
pinia4vue2 , cp4vue2 |
import { createPinia, PiniaVuePlugin } from 'pinia'; Vue.use(PiniaVuePlugin); const pinia = createPinia(); |
Create a pinia instance for Vue 2 |
defineStore , ds |
import { defineStore } from 'pinia'; export const useFileNameStore = defineStore('file-name', () => { return { } }); |
Defining Stores - Composing |
defineStore , dso |
import { defineStore } from 'pinia'; export const useFileNameStore = defineStore('file-name', { state: () => ({ }), getters: { }, actions: { }, }); |
Defining Stores - Option |
ims |
import { useFeatureStore } from '@/stores/feature'; const featureStore = useFeatureStore(); S |
Import Store |
imstr |
import { storeToRefs } from 'pinia'; const { properties } = storeToRefs(store); |
Import storeToRefs() |
imms |
import { mapState } from 'pinia'; ...mapState(useFeatureStore, ['state/getter']), |
Import mapState |
imma |
import { mapActions } from 'pinia'; ...mapActions(useFeatureStore, ['actions']), |
Import mapActions |
immws |
import { mapWritableState } from 'pinia';
...mapWritableState(useFeatureStore, ['state/getter']), |
Import mapWritableState |
ms |
...mapState(useFeatureStore, ['state/getter']), |
Usage with the Options API - Without setup() - mapState() |
ma |
...mapActions(useFeatureStore, ['actions']), |
Usage with the Options API - Without setup() - mapActions() |
mws |
...mapWritableState(useFeatureStore, ['state/getter']), |
Usage with the Options API - Without setup() - mapWritableState() |
store.$patch , sp |
featureStore.$patch((state) => { }); |
Mutating the state: store.$patch |
store.$subscribe , ss |
featureStore.$subscribe((mutation, state) => { }); |
Subscribing to state: store.$subscribe |
store.$onAction , sa |
const unsubscribe = featureStore.$onAction( ({ name, store, args, after, onError }) => { after((result) => { }); onError((error) => { }); } ); |
Subscribing to actions: store.$onAction |
store2composition , us |
const featureStore = useFeatureStore(); |
Store @ Composition API, useFeatureStore |
store2option , uso |
setup() { const featureStore = useFeatureStore() return { featureStore } }, |
Store @ Options API, useFeatureStore |