Skip to content

H. 前端 Store 速查 ​

概述

前端使用 Pinia 进行全局状态管理,核心 Store 定义在 ui/src/store/modules/ 目录下。

userStore ​

文件:store/modules/user.ts

状态 ​

字段类型说明
tokenstringJWT Token
usernamestring用户名
avatarstring头像 URL
permissionsstring[]权限列表
infoobject用户信息

Actions ​

Action说明返回
login(userInfo)登录Promise
GetInfo()获取用户信息Promise
logout()登出Promise

使用示例 ​

typescript
import { useUserStore } from '@/store/modules/user';

const userStore = useUserStore();

// 登录
await userStore.login({ username: 'admin', password: '123456' });

// 获取用户信息
const info = userStore.getUserInfo;
const permissions = userStore.getPermissions;

// 登出
await userStore.logout();

asyncRouteStore ​

文件:store/modules/asyncRoute.ts

状态 ​

字段类型说明
menusarray菜单列表
keepAliveComponentsstring[]缓存的组件名
isDynamicAddedRouteboolean是否已添加动态路由

Actions ​

Action说明
generateRoutes(userInfo)从后端获取菜单并生成路由
setDynamicAddedRoute(flag)设置动态路由添加状态
setKeepAliveComponents(components)设置 keep-alive 组件列表

projectSettingStore ​

文件:store/modules/projectSetting.ts

字段说明
navMode导航模式(侧边/顶部)
navTheme导航主题(暗色/亮色)
isShowMenuBar是否显示菜单栏
isCollapse菜单是否折叠

ossConfigStore ​

文件:store/modules/ossConfig.ts

字段说明
ossConfig云存储配置

路由守卫中初始化,用于文件上传。


designSettingStore ​

文件:store/modules/designSetting.ts

字段说明
appTheme主题色
darkMode是否暗色模式

tabsViewStore ​

文件:store/modules/tabsView.ts

字段说明
tabsList标签页列表
Action说明
addTab(route)添加标签
removeTab(route)关闭标签
closeOtherTabs(route)关闭其他标签
closeAllTabs()关闭所有标签

在组件外使用 ​

typescript
// 在 setup 外使用 Store
import { useUserStoreWidthOut } from '@/store/modules/user';

const userStore = useUserStoreWidthOut();

小蚂蚁云团队 · 提供技术支持