index.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { defineStore, acceptHMRUpdate } from "pinia";
  2. import { RouteRecordRaw } from "vue-router";
  3. export const useStore = defineStore({
  4. id: "storeInfo",
  5. state: () => ({
  6. userInfo: { id: '' }, // 当前的用户信息
  7. routers: [], // 返回的所有路由
  8. asyncRoutesMark: false, // 是否添加过路由
  9. }),
  10. getters: {
  11. getRoutersList(): RouteRecordRaw[] { // 取值
  12. return this.routers;
  13. },
  14. getToken(): string {
  15. return this.userInfo?.id || ''
  16. }
  17. },
  18. actions: {
  19. // 方法
  20. setRouters(arr: any) {
  21. this.routers = arr;
  22. },
  23. setAsyncRoutesMark(val: boolean) {
  24. this.asyncRoutesMark = val;
  25. },
  26. setValue(val: any, key: 'userInfo' | 'routers' | 'asyncRoutesMark') {
  27. this[key] = val;
  28. },
  29. clearStore() {
  30. localStorage.clear();
  31. sessionStorage.clear();
  32. this.userInfo = { id: '' };
  33. this.routers = [];
  34. this.asyncRoutesMark = false;
  35. }
  36. },
  37. persist: true, // 是否持久化
  38. });
  39. if (import.meta.hot) {
  40. import.meta.hot.accept(acceptHMRUpdate(useStore, import.meta.hot));
  41. }