import { defineStore, acceptHMRUpdate } from "pinia"; export const useStore = defineStore< string, SotreState, SoreGetters, SotreActions >("storeInfo", { state: () => ({ userInfo: {}, // 当前的用户信息 routers: [], // 返回的所有路由 asyncRoutesMark: false, // 是否添加过路由 }), getters: { getRoutersList() { return this.routers; }, getToken() { return this.userInfo?.id || ""; } }, actions: { // 方法 setRouters(arr) { this.routers = arr; }, setAsyncRoutesMark(val) { this.asyncRoutesMark = val; }, setValue(val, key) { this[key] = val; }, getRouterConfig(path) { return this.routers.find((item) => item.path === path); }, getFunctionList(path) { const config = this.getRouterConfig(path); if (!config) { return []; } return config.functionList || []; }, getUserInfoVal(val) { return this.userInfo[val]; }, clearStore() { localStorage.clear(); sessionStorage.clear(); this.userInfo = {}; this.routers = []; this.asyncRoutesMark = false; }, }, persist: { storage: sessionStorage, // 存储在 sessionStorage 中 }, // 是否持久化 }); if (import.meta.hot) { import.meta.hot.accept(acceptHMRUpdate(useStore, import.meta.hot)); }