32 lines
869 B
TypeScript
32 lines
869 B
TypeScript
import { defineStore } from 'pinia'
|
||
|
||
// 你可以任意命名 `defineStore()` 的返回值,但最好使用 store 的名字,同时以 `use` 开头且以 `Store` 结尾。
|
||
// (比如 `useUserStore`,`useCartStore`,`useProductStore`)
|
||
// 第一个参数是你的应用中 Store 的唯一 ID。
|
||
export const useAlertsStore = defineStore('alerts', {
|
||
// 其他配置...
|
||
})
|
||
export const useCounterStore = defineStore('counter', {
|
||
state: () => ({ count: 0 }),
|
||
getters: {
|
||
double: (state) => state.count * 2,
|
||
},
|
||
actions: {
|
||
increment() {
|
||
this.count++
|
||
},
|
||
decrement() {
|
||
this.count--
|
||
}
|
||
},
|
||
// 开启缓存
|
||
persist: {
|
||
enabled: true,
|
||
strategies: [
|
||
{
|
||
key: 'counter',
|
||
storage: localStorage,
|
||
},
|
||
],
|
||
},
|
||
}) |