feature:先学一点,明天学demo,学完自己练个手就差不多了

This commit is contained in:
2022-04-27 23:57:05 +08:00
parent bcc97b05d1
commit ca37a1831f
4 changed files with 71 additions and 59 deletions

28
src/store/index.ts Normal file
View File

@@ -0,0 +1,28 @@
// 1.定义容器
import { defineStore } from "pinia";
// 参数1 容器id必须唯一将来pinia会把所有的容器挂载到根容器
// 参数2 选项对象
export const userMainStore = defineStore('main', {
// 类似于data
// 必须是函数: 这样是为了服务端渲染的时候表面交叉请求导致的数据状态污染???
// 必须是箭头函数为了更好的ts的类型推断
state: () => ( { count: 50, foo: 'ccc' } ),
getters: {
// 函数接受一个可选参数state状态对象
count10( state ) {
return state.count+5
},
},
actions: {
// 不能使用箭头函数this会绑定到外部去
changeState() {
this.count++;
this.foo += '='
}
}
})
// 2.使用容器中的state
// 3.修改state
// 4.容器中action的使用