Pinia 存储库

Pinia 是 Vue 的存储库

Pinia 属性 state, getters, actions

基本示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 在这里定义 stores/counter.js
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => {
return { count: 0 }
},
// 也可以定义为
// state: () => ({ count: 0 })
actions: {
increment() {
this.count++
},
},
})

// 使用=================================
import { useCounterStore } from '@/stores/counter'
export default {
setup() {
const counter = useCounterStore()

counter.count++
// 带自动补全
counter.$patch({ count: counter.count + 1 })
// 或使用 action 代替
counter.increment()
},
}

其他

pinia状态管理