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

Vue3 之pinia状态管理

一、文档

https://pinia.vuejs.org/zh/introduction.html

二、简单使用

main.js

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
const app = createApp(App)
app.use(router)
app.use(createPinia())
app.mount('#app')

store

import {defineStore} from 'pinia'
const useCountStore = defineStore('counter', {state: ()=>{return {count: 0}},getters: {getCount(state){return state.count}},actions: {add(){this.count++}}
})export default useCountStore

vue

<script setup>
import { ref, computed } from 'vue'
import useCountStore from '../store';
import { storeToRefs } from 'pinia';const store = useCountStore()
const msg = ref('vue')// const count = computed(() => {
//   return store.count
// })

const { count } = storeToRefs(store)const add = () => {store.add()console.log(store.getCount)
}
</script><template><h1>{{ msg }}</h1><div class="card"><button type="button" @click="add()">count is {{ count }}</button></div>
</template><style scoped></style>

 

http://www.hskmm.com/?act=detail&tid=29609

相关文章:

  • 乐理 -01识谱
  • shader func
  • 案例分析-DDOS攻击、网络延迟(延迟确认纳格算法)、NAT延迟
  • 服务器丢包分析-iptables规则-MTU大小设置错误-perf-火焰图分析处理请求时内核线程调用
  • luogu P4513 小白逛公园
  • 2025 年碟式离心机厂家 TOP 企业品牌推荐排行榜,DB640 系列 / DB330 系列 / DB440 系列 / DB460 系列 / DB550 系列 / 专业碟式离心机推荐这十家公司!
  • 20231408徐钰涵课程思维导图Openssl实践
  • 案例分析-DNS+tcpdump+wireshark
  • 2025 年卧式离心机厂家 TOP 企业品牌推荐排行榜,LW250/LW350/LW450/LW530/LW540 / 专业卧式离心机推荐这十家公司!
  • 2025 年水泥管厂家最新推荐排行榜,国标水泥管,二级水泥管,钢筋混凝土水泥管,大口径水泥管,平口水泥管公司推荐!
  • Day1 经典Holle word
  • 内存知识总结
  • 2025 年金属复合板厂家推荐广东粤洋建材科技有限公司,实力产能与定制服务全景解析金属复合板公司推荐
  • 2025 年铝蜂窝板厂家最新推荐排行榜,铝蜂窝板,铝蜂窝吊顶,铝蜂窝墙面板,微孔吸音板,防火A级铝复合板公司推荐
  • 读书笔记:关于Oracle里的“老古董”:LONG类型
  • 致技术社区的英雄们:一场关于文明未来的建造邀请
  • AI图片生成思路
  • SCP/NOIP 复习:插板法
  • 内存泄漏与SWAP
  • 今天开通自己的博客啦,加油加油!成为合格的牛马! - Irving11
  • 2025安全光栅厂家最新权威推荐榜:精准防护与高效性能的工业
  • 分块学习笔记
  • 2025年10月精加工车间恒温恒湿系统厂家推荐:精准控温与高效节能首
  • 977. 有序数组的平方 双指针
  • 完整教程:iSCSI服务器
  • 深入解析:数据库视图:虚拟表的强大应用
  • agc001_c题解
  • 【IMU】6轴数据校准算法
  • 2025 年 MES 服务商 TOP 平台机构推荐排行榜,mes 系统 /mes 软件 /mes 制造执行系统 /mes 生产制造执行系统 /mes 生产管理系统公司推荐
  • 2025 年10月 WMS 服务商最新推荐榜单,wms系统 wms软件,wms仓库管理软件,wms仓库管理系统软件公司推荐