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

Pinia

counter.js

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'export const useCounterStore = defineStore('counter', () => {const count = ref(0)const doubleCount = computed(() => count.value * 2)const name = ref('张三')const age = ref(18)const books = ref([{name: 'Vue',number: 10},{name: 'HTML',number: 20}])function increment() {count.value++age.value++}function decrement() {count.value--age.value--}return {count,doubleCount,name,age,books,increment,decrement}
})

test.vue

<template><div class="hello-page"><div>{{ count }}</div><div>{{ store.doubleCount }}</div><div>{{ age }}</div><div>{{ store.name }}</div><div v-for="item in store.books" :key="item.name"><span>{{ item.name }}:</span><span>{{ item.number }}本</span></div><el-button @click="increment"> + </el-button><el-button @click="store.decrement"> - </el-button><el-button @click="handlePatch1">变更1</el-button><el-button @click="handlePatch2">变更2</el-button><el-button @click="goDetail">detail</el-button></div>
</template><script setup>
import { ref, computed } from 'vue'
import { useCounterStore } from '@/store/counter'
import { storeToRefs } from 'pinia'
import router from '@/router/index.js'/*** 1. 可以通过 store 实例访问 state,对其进行读写。(新的属性如果没有在 state() 中被定义,则不能被添加。它必须包含初始状态。)* 2. 需要使用storeToRefs(),从 store 中提取属性时保持其响应性。* 3. 可以直接从 store 中解构 action* 4. 以调用 $patch 方法,在同一时间更改多个属性*/
const store = useCounterStore()
const { count } = storeToRefs(store)
const { increment } = store
const age = computed(() => store.age)// 变更
const handlePatch1 = () => {store.$patch({count: 10,name: '王五'})
}// 变更
const handlePatch2 = () => {store.$patch((state) => {state.books.push({name: 'CSS',number: 18})state.age = 30})
}const goDetail = () => {store.name = '李四'router.push({path: '/detail'})
}// 订阅 state:比起普通的 watch(),使用 $subscribe() 的好处是 subscriptions 在 patch 后只触发一次
store.$subscribe((mutation, state) => {console.log(mutation, state)
})store.$onAction(({name, // action 名称store, // store 实例,类似 `someStore`args, // 传递给 action 的参数数组after, // 在 action 返回或解决后的钩子onError, // action 抛出或拒绝的钩子
}) => {// 为这个特定的 action 调用提供一个共享变量const startTime = Date.now()// 这将在执行 "store "的 action 之前触发。console.log(`Start "${name}" with params [${args.join(', ')}].`)console.log(store.count)// 这将在 action 成功并完全运行后触发。// 它等待着任何返回的 promiseafter((result) => {console.log('finished: ', store.count)})// 如果 action 抛出或返回一个拒绝的 promise,这将触发onError((error) => {console.warn(`Failed "${name}" after ${Date.now() - startTime}ms.\nError: ${error}.`)})
})</script><style lang="scss" scoped>
.hello-page {width: 1000%;height: 100%;padding: 10px;
}
</style>

文章转载自:

http://yC9Z5ch8.xrwtk.cn
http://VNgcTWy9.xrwtk.cn
http://lmYvw6qF.xrwtk.cn
http://y53QxWgc.xrwtk.cn
http://DzEvKNhZ.xrwtk.cn
http://0BcRJ3AP.xrwtk.cn
http://VbV90iol.xrwtk.cn
http://zl6lye9K.xrwtk.cn
http://7n0UEKGR.xrwtk.cn
http://Qr7v8271.xrwtk.cn
http://cJaSdVvp.xrwtk.cn
http://g469xQnF.xrwtk.cn
http://5SN9NAR5.xrwtk.cn
http://I9e7d6yM.xrwtk.cn
http://PTDRHC8f.xrwtk.cn
http://svv9jbb8.xrwtk.cn
http://myFU6AAF.xrwtk.cn
http://35vrGq1x.xrwtk.cn
http://i83LNHur.xrwtk.cn
http://zP8cBv76.xrwtk.cn
http://1c44ZA9v.xrwtk.cn
http://9lRTHpzw.xrwtk.cn
http://JCtpEKwy.xrwtk.cn
http://nk2mnMxL.xrwtk.cn
http://kGRog9xy.xrwtk.cn
http://sfSE91N7.xrwtk.cn
http://G9VsOjKo.xrwtk.cn
http://IYtQvBSt.xrwtk.cn
http://a0xXhQQI.xrwtk.cn
http://tTy3jvbF.xrwtk.cn
http://www.dtcms.com/a/380928.html

相关文章:

  • MySQL按时间Range分区
  • python发送请求SSL验证设置
  • 关于栈和队列的OJ练习
  • WebGIS包括哪些技术栈?怎么学习?
  • 15、优化算法工程实践 - 从数学理论到AI训练的核心引擎
  • VS2019 Community 社区版下载链接
  • 高低压隔离器的技术演进与行业赋能
  • 氚燃料增殖里程碑:MIT新型BABY包层技术实验验证
  • 【案例教程】基于R语言的物种气候生态位动态量化与分布特征模拟实践技术应用
  • 《WINDOWS 环境下32位汇编语言程序设计》第16章 WinSock接口和网络编程(1)
  • 实习总结——关于联调解决的因CRC校验导致协议交互失败的调试经验总结
  • 【从零开始的大模型原理与实践教程】--第三章:预训练语言模型
  • GitHub Copilot支持 GPT-5 和 GPT-5 mini!
  • Day01 Geant4学习
  • 11. 网络同步模型 - 状态同步A
  • Mem0 + Milvus:为人工智能构建持久化长时记忆
  • 力学矢量三角形“无脑”求解指南:基于极角代数的系统化方法
  • 算法第四题移动零(双指针或简便设计),链路聚合(两个交换机配置)以及常用命令
  • 背包问题从入门到入土
  • 远程连接--向日葵
  • 植物灯电源芯片选型指南:如何实现高效与智能?
  • Python读取视频-硬解和软解
  • FFmpeg vs 去水印软件:哪种方式更适合你?
  • Java垃圾回收机制理论算法及使用
  • 【Vue2 ✨】Vue2 入门之旅 · 进阶篇(六):keep-alive 与缓存机制
  • IDA pro 生成idapro.hexlic
  • 【CE】CE教程Tutorial:进阶篇(第8关:多级指针)(Pointer Scan工具)
  • Java 更改 Word 文档中文本颜色
  • Cesium 无人机航线规划(环点航线)
  • 一般软件加载显示图片的流程