当前位置: 首页 > news >正文

包教包会vue3+ts状态管理工具pinia

一、Pinia介绍

定义:pinia是和vuex一样的状态管理工具

语法:和 Vue3 一样,它实现状态管理有两种语法:选项式API 和 组合式API

支持:vue2、typeScript、devtools

二、使用步骤

1.安装

pnpm add pinia

yarn add pinia

npm i pinia

2.在main.ts中导入,实例化

// 导入pinia
import { createPinia } from 'pinia'
const pinia = createPinia()// 挂载pinia
createApp(App).use(pinia).mount('#app')

3.创建pinia仓库并使用

(1)组合式API写法

创建:

import { defineStore } from 'pinia'
// import * as obj from 'pinia'  
// console.log(obj);   
import { ref } from 'vue'
/* 1.组合式pinia */
export const userStoreHr = defineStore('hr', () => {// (1)模拟statelet num = ref(100)const arr = ref([1, 2, 3, 4, 5])// (2)模拟mutationconst changeNum = () => {num.value += 100}// (3)模拟actionconst activeArr = () => {setTimeout(() => {const n = Math.floor(Math.random() * 10) + 1arr.value.push(n)}, 1000)}// (4)模拟gettersconst total = () => {return arr.value.reduce((sum, item) => item + sum, 0)}return { num, changeNum, activeArr, total }
})export default userStoreHr

使用:

<script setup lang="ts">
import { userStoreHr } from './store/hr'
const store = userStoreHr()
// console.log(store);
</script><template><div><div>app.vue</div><div>num:{{ store.num }} total:{{ store.total() }}</div><button @click="store.changeNum()">按钮1</button><button @click="store.activeArr()">按钮2</button>
</div>
</template>

(2)选项式API写法

创建:

// 创建pinia仓库
import { defineStore } from "pinia";export const useStoreTt = defineStore('tt', {state: () => {return {count: 10,price: 50}},actions: {addPrice() {this.price += 1console.log(this, '组合式API可以用this');}},getters: {/* 以下两种写法都可以 */// total(): number {//   return this.count * this.price// }  total: (state) => {return state.count * state.price}},
})export default useStoreTt

使用:

<script setup lang="ts">
import { useStoreTt } from './store/tt'
const store = useStoreTt()</script><template><div><div>app.vue</div><div>count :{{ store.count }} price : {{ store.price }}</div><div>total :{{ store.total }}</div><button @click="store.addPrice()">addPrice</button>
</div>
</template>

(3)storeToRefs的使用

解决:解构数据后,响应式失效的问题(不能解构函数)

<script setup lang="ts">
import { useStoreTt } from './store/tt'
// 导入storeToRefs 
import { storeToRefs } from 'pinia';
const store = useStoreTt()
// 解构的时候调用storeToRefs 
const { count, price } = storeToRefs(store)</script><template><div><div>app.vue</div><!-- 模板中直接使用 --><div>count :{{ count }} price : {{ price }}</div><div>total :{{ store.total }}</div><button @click="store.addPrice()">addPrice</button>
</div>
</template>

http://www.lryc.cn/news/10117.html

相关文章:

  • Generated columns cannot be used in COPY
  • Amazon S3简介
  • MySQL索引类型——有五种
  • CloudCompare 二次开发(5)——非插件中的PCL环境配置(均匀采样为例)
  • 停车辅助系统的技术和变化
  • 扬帆优配|日均客运量恢复,民航业加速复苏,外资买入2股超亿元
  • 【PyTorch】教程:torch.nn.ModuleDict
  • Git、小乌龟、Gitee的概述与安装应用超详细(组长与组员多人开发版本)
  • 【java 高并发编程之JUC】高阶JUC特性总结
  • 行业分析| 智能无人自助设备
  • 使用契约测试得不偿失?试试契约先行开发
  • 函数编程之Function
  • Vue 双向绑定原理
  • 【数据治理-03】无规矩不成方圆,聊聊如何建立数据标准
  • dos常用命令
  • 解决原生template标签在Vue中失效的问题
  • 节能降耗方案-医院能源管理系统平台的研究与应用分析
  • Redis学习【7】之发布_订阅命令和事务
  • MySQL8.0 optimizer_switch变化
  • Web--Maven
  • 深入理解MySQLⅢ -- 锁与InnoDB引擎
  • Win11电脑速度慢、延迟高怎么办?
  • 【双指针问题】977. 有序数组的平方
  • Meta AR眼镜主管:正开发史无前例的AR,但要解决很多困难
  • Docker 搭建KingbaseES主备流复制
  • java易错题锦集四
  • 每天10个前端小知识 【Day 17】
  • Python语言零基础入门教程(二十三)
  • [ansible系列]ansible使用扩展
  • Java工具类(时间格式转换)