news 2026/4/16 14:42:16

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/4/16 7:22:44

英雄联盟个性化美化神器:LeaguePrank完全使用指南

英雄联盟个性化美化神器&#xff1a;LeaguePrank完全使用指南 【免费下载链接】LeaguePrank 项目地址: https://gitcode.com/gh_mirrors/le/LeaguePrank 想要在英雄联盟中展现与众不同的游戏形象吗&#xff1f;LeaguePrank是一款功能强大的LOL美化工具&#xff0c;通过…

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

MediaPipe Holistic参数调优:关键点过滤阈值设置

MediaPipe Holistic参数调优&#xff1a;关键点过滤阈值设置 1. 引言&#xff1a;AI 全身全息感知的技术挑战 随着虚拟现实、数字人和智能交互系统的快速发展&#xff0c;对全维度人体动作捕捉的需求日益增长。传统的单模态检测&#xff08;如仅姿态或仅手势&#xff09;已无…

作者头像 李华
网站建设 2026/4/16 1:06:13

5分钟搞定纪念币预约的终极秘籍

5分钟搞定纪念币预约的终极秘籍 【免费下载链接】auto_commemorative_coin_booking 项目地址: https://gitcode.com/gh_mirrors/au/auto_commemorative_coin_booking 还在为抢不到心仪的纪念币而emo吗&#xff1f;每次打开预约页面&#xff0c;看着秒光的库存&#xff…

作者头像 李华
网站建设 2026/4/16 7:29:00

Holistic Tracking部署监控:性能指标可视化教程

Holistic Tracking部署监控&#xff1a;性能指标可视化教程 1. 引言 1.1 业务场景描述 在虚拟现实、数字人驱动、远程交互和智能监控等前沿应用中&#xff0c;对人类行为的全面理解已成为核心技术需求。传统的单模态感知&#xff08;如仅姿态或仅手势&#xff09;已无法满足…

作者头像 李华
网站建设 2026/4/15 17:20:26

Holistic Tracking降本部署案例:CPU极速版节省90%算力成本

Holistic Tracking降本部署案例&#xff1a;CPU极速版节省90%算力成本 1. 技术背景与业务挑战 在虚拟人、元宇宙、AI直播等前沿应用场景中&#xff0c;对全维度人体动作捕捉的需求日益增长。传统方案通常依赖多模型并行推理——分别运行人脸、手势和姿态模型&#xff0c;不仅…

作者头像 李华
网站建设 2026/4/16 2:54:15

screen+实现多语言HMI界面:项目应用

用 screen 构建多语言HMI&#xff1a;一次搞定全球部署的实战方案你有没有遇到过这样的场景&#xff1f;一台设备刚在德国交付&#xff0c;客户突然要求下周发往日本——界面语言怎么办&#xff1f;重写代码&#xff1f;重新编译下载&#xff1f;还是干脆再做一套固件&#xff…

作者头像 李华