news 2026/4/2 21:50:07

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/3/31 11:44:43

MediaPipe Holistic性能测试:CPU环境下的极限挑战

MediaPipe Holistic性能测试&#xff1a;CPU环境下的极限挑战 1. 引言&#xff1a;AI 全身全息感知的技术演进 随着虚拟现实、数字人和智能交互系统的快速发展&#xff0c;对全维度人体行为理解的需求日益增长。传统方案往往依赖多个独立模型分别处理人脸、手势与姿态&#x…

作者头像 李华
网站建设 2026/3/30 13:54:42

BiliTools终极指南:免费跨平台哔哩哔哩下载工具完整使用教程

BiliTools终极指南&#xff1a;免费跨平台哔哩哔哩下载工具完整使用教程 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱&#xff0c;支持视频、音乐、番剧、课程下载……持续更新 项目地址: https://gitcode.com/GitHub_Trending/bili…

作者头像 李华
网站建设 2026/3/30 3:23:25

科哥推荐的IndexTTS2使用技巧,新手少走弯路

科哥推荐的IndexTTS2使用技巧&#xff0c;新手少走弯路 1. 引言&#xff1a;为什么你需要关注 IndexTTS2 的正确使用方式 随着语音合成技术的快速发展&#xff0c;IndexTTS2 凭借其在情感控制方面的显著提升&#xff0c;成为当前中文 TTS 领域备受关注的开源项目之一。特别是…

作者头像 李华
网站建设 2026/4/2 7:52:20

MMD Tools插件完全指南:5个步骤实现Blender与MMD的完美融合

MMD Tools插件完全指南&#xff1a;5个步骤实现Blender与MMD的完美融合 【免费下载链接】blender_mmd_tools MMD Tools is a blender addon for importing/exporting Models and Motions of MikuMikuDance. 项目地址: https://gitcode.com/gh_mirrors/bl/blender_mmd_tools …

作者头像 李华
网站建设 2026/3/25 4:55:01

3步搞定B站视频下载:BiliTools让你的媒体管理效率翻倍

3步搞定B站视频下载&#xff1a;BiliTools让你的媒体管理效率翻倍 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱&#xff0c;支持视频、音乐、番剧、课程下载……持续更新 项目地址: https://gitcode.com/GitHub_Trending/bilit/Bili…

作者头像 李华
网站建设 2026/3/24 1:32:41

纪念币智能预约:高效抢购的全新解决方案

纪念币智能预约&#xff1a;高效抢购的全新解决方案 【免费下载链接】auto_commemorative_coin_booking 项目地址: https://gitcode.com/gh_mirrors/au/auto_commemorative_coin_booking 还在为每次纪念币预约失败而困扰吗&#xff1f;这款纪念币预约自动化工具正是你需…

作者头像 李华