当前位置: 首页 > 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.zskr.cn/news/19734.html

相关文章:

  • 乐理 -01识谱
  • 服务器丢包分析-iptables规则-MTU大小设置错误-perf-火焰图分析处理请求时内核线程调用
  • 2025 年水泥管厂家最新推荐排行榜,国标水泥管,二级水泥管,钢筋混凝土水泥管,大口径水泥管,平口水泥管公司推荐!
  • Day1 经典Holle word
  • 2025 年金属复合板厂家推荐广东粤洋建材科技有限公司,实力产能与定制服务全景解析金属复合板公司推荐
  • 致技术社区的英雄们:一场关于文明未来的建造邀请
  • SCP/NOIP 复习:插板法
  • 完整教程:iSCSI服务器
  • 非常好的学习方式是哪样
  • 【Trie】 UVA1401 Remember the Word - 教程
  • 2025 年 MES 服务商 TOP 平台机构推荐排行榜,mes 系统 /mes 软件 /mes 制造执行系统 /mes 生产制造执行系统 /mes 生产管理系统公司推荐
  • 2025 年10月 WMS 服务商最新推荐榜单,wms系统 wms软件,wms仓库管理软件,wms仓库管理系统软件公司推荐
  • Matlab凭借GUI实现点云的中值滤波(附最简版)
  • CF数据结构题做题记录-1
  • CCPC2023女生专场 游记(VP)
  • tp3.2不再生成Runtime/Logs日志
  • 心得:刷算法的痛点-只根据题目的case思考,不考虑边界情况,写出一坨shit
  • 2.4 DQN 变体(Rainbow)
  • Emacs折腾日记(三十二)——org mode的基本美化
  • pp
  • vim配置使用
  • shell展开shell数组
  • 原木
  • 2025年10月镀锌卷板厂家最新推荐排行榜,有花镀锌卷板,无花镀锌卷板,高锌层镀锌卷板,批发镀锌卷板公司推荐
  • 会话跟踪方案
  • 阻塞、非阻塞、同步、异步的区别是什么?
  • Linux文本编辑三剑客之grep
  • Linux文本编辑三剑客之awk
  • 详细介绍:【ROS2学习笔记】节点篇:节点概述
  • 2025.10.12 - 20243867孙堃2405