当前位置:网站首页>pinia的基本使用
pinia的基本使用
2022-07-22 23:38:00 【山竹回家了】
- 没有mutation、没有嵌套模块,相对vuex对ts有更好的支持
一、安装
npm install pinia
二、初始化配置
src\main.ts
import {
createApp } from 'vue'
import './style.css'
import App from './App.vue'
import {
createPinia } from 'pinia'
// 创建pinia实例
const pinia = createPinia()
const app = createApp(App)
// 挂载到vue根实例
app.use(pinia)
app.mount('#app')
三、基本使用
3.1创建store并使用state的值
- src\store\index.ts
import {
defineStore } from "pinia";
// 1.定义并导出容器
//参数1:容器的ID,必须唯一,将来pinia会把所有的容器挂载到根容器
//参数2:选项对象
//返回值:一个函数,调用得到容器实例
export const useMainStore = defineStore('main', {
/** * 类似组件的data,用来储存全局状态的 * 1.必须是函数:这样是为了服务端渲染的时候避免交叉请求导致的数据状态污染 * 2.必须是箭头函数 */
state: () => {
return {
count: 19
}
},
/** * 类似于组件的computed,用来封装计算属性,有缓存的功能 */
getters: {
},
/** * 类似于组件的methods,封装业务逻辑,修改state */
actions: {
}
})
- src\components\HelloWorld.vue
- 注意直接不要解构拿值,否则不是响应式,要使用官方的storeToRefs()
<script setup lang="ts">
import {
storeToRefs } from "pinia";
import {
useMainStore } from "../store/index";
const mainStore = useMainStore();
const {
count } = storeToRefs(mainStore);
console.log(mainStore.count);
const handleChangState = () => {
mainStore.count++;
};
</script>
<template>
{
{
mainStore.count }}=============={
{
count }}
<button @click="handleChangState">按钮</button>
</template>
<style scoped>
.read-the-docs {
color: #888;
}
</style>

3.2状态更新与actions
- 多个数据修改的情况下,使用$patch进行批量修改性能好
src\store\index.ts
import {
defineStore } from "pinia";
// 1.定义并导出容器
//参数1:容器的ID,必须唯一,将来pinia会把所有的容器挂载到根容器
//参数2:选项对象
//返回值:一个函数,调用得到容器实例
export const useMainStore = defineStore('main', {
/** * 类似组件的data,用来储存全局状态的 * 1.必须是函数:这样是为了服务端渲染的时候避免交叉请求导致的数据状态污染 * 2.必须是箭头函数 */
state: () => {
return {
count: 19
}
},
/** * 类似于组件的computed,用来封装计算属性,有缓存的功能 */
getters: {
},
/** * 类似于组件的methods,封装业务逻辑,修改state */
actions: {
handleState(num: number) {
this.count += num
}
}
})
src\components\HelloWorld.vue
<script setup lang="ts">
import {
storeToRefs } from "pinia";
import {
useMainStore } from "../store/index";
const mainStore = useMainStore();
const {
count } = storeToRefs(mainStore);
const handleChangState = () => {
// 方法一:
// mainStore.count++;
// 方法二:
// mainStore.$patch({
// count: mainStore.count + 1,
// });
// 方法三:
// mainStore.$patch((state) => {
// state.count++;
// });
// 方法四:
mainStore.handleState(10);
};
</script>
<template>
{
{
mainStore.count }}=============={
{
count }}
<button @click="handleChangState">按钮</button>
</template>
<style scoped>
.read-the-docs {
color: #888;
}
</style>
3.3getters
src\store\index.ts
import {
defineStore } from "pinia";
// 1.定义并导出容器
//参数1:容器的ID,必须唯一,将来pinia会把所有的容器挂载到根容器
//参数2:选项对象
//返回值:一个函数,调用得到容器实例
export const useMainStore = defineStore('main', {
/** * 类似组件的data,用来储存全局状态的 * 1.必须是函数:这样是为了服务端渲染的时候避免交叉请求导致的数据状态污染 * 2.必须是箭头函数 */
state: () => {
return {
count: 19
}
},
/** * 类似于组件的computed,用来封装计算属性,有缓存的功能 */
getters: {
count10(state) {
console.log('看一下有没有被缓存')
return state.count + 10
}
},
/** * 类似于组件的methods,封装业务逻辑,修改state */
actions: {
}
})
<script setup lang="ts">
import {
storeToRefs } from "pinia";
import {
useMainStore } from "../store/index";
const mainStore = useMainStore();
</script>
<template>
{
{
mainStore.count10 }}
{
{
mainStore.count10 }}
{
{
mainStore.count10 }}
</template>
<style scoped>
.read-the-docs {
color: #888;
}
</style>

边栏推荐
- [GNN report] Li Jia, Hong Kong University of science and technology: Rethinking graph anomaly detection - what kind of graph neural network do we need?
- 图的遍历 ~
- 什么是NFT?你不会还不知道吧!
- Program environment and pretreatment
- 聊聊队列(FIFO)的应用
- 程序员可能还是程序员,码农可能只能是码农了
- Add payment method after successful registration of Alibaba cloud international edition
- Talking about -- network security architecture design (4)
- flink使用ListState实现KeyedState
- 【arXiv2022】GroupTransNet: Group Transformer Network for RGB-D Salient Object Detection
猜你喜欢

MySQL和Navicat的安装与配置

【arXiv2022】GroupTransNet: Group Transformer Network for RGB-D Salient Object Detection

XMODEM, ymodem and zmodem protocols are the three most commonly used communication protocols

算法---二维数组网格迁移(Kotlin)

浅谈——网路安全架构设计(一)

outlook客户端 outlook.com邮箱设置方法

程序环境和预处理

1.学会看懂网页

MySQL 分库分表及其平滑扩容方案
![[arxiv2022] grouptransnet: Group transformer Network for RGB - D Salient Object Detection](/img/be/534f92578f0d825f9b565c1785af18.png)
[arxiv2022] grouptransnet: Group transformer Network for RGB - D Salient Object Detection
随机推荐
08 爬虫项目
笔试强训第21天
On the stability of common sorting
flink使用ListState实现KeyedState
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
【GNN报告】华为诺亚实验室周敏:曲率视角下的图数据建模与分析
5分钟实现微信小程序绘制二维码
Golang中iota的正确用法
C语言编写“Hello World”挑战赛,你会如何作答?
Comment synchroniser
PKS的秘书&兄弟 | 温故知新
Dark horse programmer - interface testing - four-day learning interface testing - the second day - Interface use case design, test points, function testing, security testing, performance testing, sing
UE5分屏(小地图)的解决方案
冰冰学习笔记:vim工具的基本操作
构造函数的初始化、清理及const修饰成员函数
Entrepreneurial documents: how to write a reminder letter
BS4 index objects by attribute index and name
【arXiv2022】GroupTransNet: Group Transformer Network for RGB-D Salient Object Detection
5 minutes to achieve wechat applet drawing QR code
What's the use of volatile