vuex 使用pinia存储数据并保持持久化
import { defineStore } from 'pinia'
// 你可以任意命名 `defineStore()` 的返回值,但最好使用 store 的名字,同时以 `use` 开头且以 `Store` 结尾。
// (比如 `useUserStore`,`useCartStore`,`useProductStore`)
// 第一个参数是你的应用中 Store 的唯一 ID。
export const useAlertsStore = defineStore('alerts', {
// 其他配置...
state: () => ({ count: 0 }),
getters: {
double: (state) => state.count * 2,
},
actions: {
increment() {
this.count++
},
},
})
//你可以认为 state 是 store 的数据 (data),getters 是 store 的计算属性 (computed),而 actions 则是方法 (methods)。
// 重点注解:此段代码解释 设定变量count初始值为0,
// 在不调用actions中的increment方法的时候直接useAlertsStore.count为0,但是如果调用increment后在获取count为2 ,因为increment导致0+1=1,getters中监听到count值发生变化,又开始执行*2,所以1*2=2,最后获取的值count是2.