feature:先学一点,明天学demo,学完自己练个手就差不多了
This commit is contained in:
28
src/store/index.ts
Normal file
28
src/store/index.ts
Normal 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的使用
|
||||
Reference in New Issue
Block a user