|
|
|
@ -0,0 +1,31 @@
|
|
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
|
|
|
|
|
// `defineStore()` 的返回值的命名是自由的
|
|
|
|
|
// 但最好含有 store 的名字,且以 `use` 开头,以 `Store` 结尾。
|
|
|
|
|
// (比如 `useUserStore`,`useCartStore`,`useProductStore`)
|
|
|
|
|
// 第一个参数是你的应用中 Store 的唯一 ID。
|
|
|
|
|
export const testStore = defineStore('testStore', {
|
|
|
|
|
// state可以理解为一个共享的内存
|
|
|
|
|
state: () => {
|
|
|
|
|
return {
|
|
|
|
|
count: 0
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// getter
|
|
|
|
|
getters:{
|
|
|
|
|
// 数据放在state里面,需要传递state
|
|
|
|
|
doubleCount(state){
|
|
|
|
|
return state.count
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// action
|
|
|
|
|
actions:{
|
|
|
|
|
// 设置state里面的数据
|
|
|
|
|
// count:number表示传递进去的count应该是一个number类型
|
|
|
|
|
setCount(count:number){
|
|
|
|
|
this.count = count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|