配置Pinia步骤:
1:安装Pinia
image.png
2:在入口文件中把Pinia挂在为Vue的插件
image.png
3:在src/stores文件夹中声明store
image.png
4:在组件中引入,并使用store
image.png
重点:起名字时使用id
在store中声明state
image.png
在组件中访问state
storeToRefs是为了都让对象变成响应式、也可以解构赋值
image.png
Getters
1、简单使用
image.png
image.png
2、访问自己store中的其他getters
image.png
image.png
image.png
3、给getters传递参数
计算属性返回一个函数传入参数
image.png
image.png
image.png
4、访问其他store中的getters
首先在使用的store中引入其他store其次在getter中使用
image.png
image.png
Store中的actions
Pinia与vuex的不同在于处理同步和异步任务都只需要actions
1、同步任务
image.png
image.png
2、异步任务
image.png