news 2026/8/6 21:19:53

Vue——Vue3 + Vite 状态管理篇 之【Pinia 状态管理架构】

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue——Vue3 + Vite 状态管理篇 之【Pinia 状态管理架构】

背景问题:
需要管理应用的全局状态。

方案思考:
使用 Pinia 作为状态管理工具。

具体实现:
创建 Pinia 实例:

// stores/index.jsimport{createPinia}from'pinia'constpinia=createPinia()exportdefaultpinia// main.jsimport{createApp}from'vue'importAppfrom'./App.vue'importpiniafrom'@/stores'constapp=createApp(App)app.use(pinia)app.mount('#app')

用户状态管理 Store 示例:

// stores/user.jsimport{defineStore}from'pinia'import{ref,computed}from'vue'exportconstuseUserStore=defineStore('user',()=>{// 定义状态constuserInfo=ref({})constisLoggedIn=ref(false)constpermissions=ref([])// 定义计算属性consthasPermission=computed(()=>{return(permission)=>{returnpermissions.value.includes(permission)}})// 定义动作constsetUser=(user)=>{userInfo.value=user isLoggedIn.value=!!user}constsetPermissions=(perms)=>{permissions.value=perms}constclearUser=()=>{userInfo.value={}isLoggedIn.value=falsepermissions.value=[]}constupdateProfile=(profile)=>{Object.assign(userInfo.value,profile)}return{// 状态userInfo,isLoggedIn,permissions,// 计算属性hasPermission,// 动作setUser,setPermissions,clearUser,updateProfile}})

在组件中使用 Store 的示例:

<template> <div class="user-profile"> <h2>用户信息</h2> <p>姓名: {{ userStore.userInfo.name }}</p> <p>邮箱: {{ userStore.userInfo.email }}</p> <p>状态: {{ userStore.isLoggedIn ? '已登录' : '未登录' }}</p> <div v-if="userStore.hasPermission('user.edit')"> <el-button @click="editProfile">编辑资料</el-button> </div> <div v-else> <p>您没有编辑权限</p> </div> <el-button @click="logout" type="danger">退出登录</el-button> </div> </template> <script setup> import { useUserStore } from '@/stores/user' const userStore = useUserStore() const editProfile = () => { console.log('编辑用户资料') // 编辑逻辑 } const logout = () => { userStore.clearUser() console.log('用户已退出') } </script>

应用状态管理 Store 示例:

// stores/app.jsimport{defineStore}from'pinia'import{ref}from'vue'exportconstuseAppStore=defineStore('app',()=>{// 侧边栏状态constsidebar=ref({opened:true,withoutAnimation:false})// 设备类型constdevice=ref('desktop')// 全屏状态constfullscreen=ref(false)// 切换侧边栏consttoggleSidebar=(withoutAnimation)=>{sidebar.value.opened=!sidebar.value.opened sidebar.value.withoutAnimation=withoutAnimation}// 关闭侧边栏constcloseSidebar=(withoutAnimation)=>{sidebar.value.opened=falsesidebar.value.withoutAnimation=withoutAnimation}// 切换设备consttoggleDevice=(deviceType)=>{device.value=deviceType}// 切换全屏consttoggleFullscreen=()=>{if(!document.fullscreenElement){document.documentElement.requestFullscreen()fullscreen.value=true}else{if(document.exitFullscreen){document.exitFullscreen()fullscreen.value=false}}}return{sidebar,device,fullscreen,toggleSidebar,closeSidebar,toggleDevice,toggleFullscreen}})
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/3 3:50:28

对比传统TTS,IndexTTS2在哪些场景更胜一筹?

对比传统TTS&#xff0c;IndexTTS2在哪些场景更胜一筹&#xff1f; 随着人机交互的不断演进&#xff0c;文本转语音&#xff08;TTS&#xff09;技术已从“能发声”迈向“懂情感”的新阶段。传统的TTS系统虽然能够完成基本的文字朗读任务&#xff0c;但在语调自然度、情感表达…

作者头像 李华
网站建设 2026/7/28 12:30:30

IndexTTS2性能优化秘籍,推理速度提升50%

IndexTTS2性能优化秘籍&#xff0c;推理速度提升50% 在当前AIGC内容创作爆发的背景下&#xff0c;文本转语音&#xff08;TTS&#xff09;系统已从“能发声”迈向“会共情”的新阶段。IndexTTS2 V23版本凭借其卓越的情感建模能力与易用性设计&#xff0c;迅速成为中文TTS领域的…

作者头像 李华
网站建设 2026/7/28 10:20:10

手把手教学:用AI证件照工坊给全家制作签证照片的完整过程

手把手教学&#xff1a;用AI证件照工坊给全家制作签证照片的完整过程 随着出国旅游、留学、探亲等需求日益增长&#xff0c;办理各类签证时对证件照的要求也愈发严格。传统照相馆不仅价格高、耗时长&#xff0c;还可能因不符合标准被拒。而市面上许多在线证件照工具又存在隐私…

作者头像 李华
网站建设 2026/7/28 16:44:47

星露谷物语XNB文件处理完全指南:从入门到精通

星露谷物语XNB文件处理完全指南&#xff1a;从入门到精通 【免费下载链接】xnbcli A CLI tool for XNB packing/unpacking purpose built for Stardew Valley. 项目地址: https://gitcode.com/gh_mirrors/xn/xnbcli 还在为星露谷物语的mod制作而烦恼吗&#xff1f;想要个…

作者头像 李华
网站建设 2026/7/29 18:40:40

戴尔G15散热控制神器:tcc-g15让你的笔记本告别高温困扰

戴尔G15散热控制神器&#xff1a;tcc-g15让你的笔记本告别高温困扰 【免费下载链接】tcc-g15 Thermal Control Center for Dell G15 - open source alternative to AWCC 项目地址: https://gitcode.com/gh_mirrors/tc/tcc-g15 还在为戴尔G15笔记本玩游戏时温度飙升而烦恼…

作者头像 李华
网站建设 2026/7/29 20:20:34

用Nginx代理IndexTTS2,外网访问更安全

用Nginx代理IndexTTS2&#xff0c;外网访问更安全 在本地部署的语音合成系统&#xff08;如 IndexTTS2&#xff09;日益普及的背景下&#xff0c;如何在保障服务可用性的同时提升安全性&#xff0c;成为团队运维和开发者关注的核心问题。尤其当 IndexTTS2 V23 版本由“科哥”构…

作者头像 李华