vuejs / pinia
- суббота, 26 марта 2022 г. в 00:34:46
🍍 Intuitive, type safe, light and flexible Store for Vue using the composition api with DevTools support
Intuitive, type safe and flexible Store for Vue
Pinia works both for Vue 2.x and Vue 3.x. It requires Vue 2 with the latest @vue/composition-api
or Vue ^3.2.0-0
.
Pinia is the most similar English pronunciation of the word pineapple in Spanish: piña. A pineapple is in reality a group of individual flowers that join together to create a multiple fruit. Similar to stores, each one is born individually, but they are all connected at the end. It's also a delicious tropical fruit indigenous to South America.
A few notes about the project and possible questions:
Q: Is Pinia the successor of Vuex?
A: Yes
Q: What about dynamic modules?
A: Dynamic modules are not type safe, so instead we allow creating different stores that can be imported anywhere
pinia
)useOtherStore()
inside of a getter or action.yarn add pinia
# or with npm
npm install pinia
If you are using Vue 2, make sure to install latest @vue/composition-api
:
npm install pinia @vue/composition-api
Create a pinia (the root store) and pass it to app:
import { createPinia } from 'pinia'
app.use(createPinia())
You can create as many stores as you want, and they should each exist in different files:
import { defineStore } from 'pinia'
// main is the name of the store. It is unique across your application
// and will appear in devtools
export const useMainStore = defineStore('main', {
// a function that returns a fresh state
state: () => ({
counter: 0,
name: 'Eduardo',
}),
// optional getters
getters: {
// getters receive the state as first parameter
doubleCount: (state) => state.counter * 2,
// use getters in other getters
doubleCountPlusOne(): number {
return this.doubleCount + 1
},
},
// optional actions
actions: {
reset() {
// `this` is the store instance
this.counter = 0
},
},
})
defineStore
returns a function that has to be called to get access to the store:
import { useMainStore } from '@/stores/main'
import { storeToRefs } from 'pinia'
export default defineComponent({
setup() {
const main = useMainStore()
// extract specific store properties
const { counter, doubleCount } = storeToRefs(main)
return {
// gives access to the whole store in the template
main,
// gives access only to specific state or getter
counter,
doubleCount,
}
},
})
To learn more about Pinia, check its documentation.