app.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import defaultSettings from '@/settings'
  2. import { defineStore } from 'pinia'
  3. import { SettingTy } from '~/common'
  4. export const useAppStore = defineStore('app', {
  5. /***
  6. *类似于组件的 data数据的 ,用来存储全局状态的
  7. * 1、必须是箭头函数
  8. */
  9. state: () => {
  10. return {
  11. sidebar: { opened: true },
  12. device: 'desktop',
  13. settings: defaultSettings as SettingTy,
  14. cachedViews: [] as Array<string>,
  15. cachedViewsDeep: [] as Array<string>
  16. }
  17. },
  18. /***
  19. *封装处理数据的函数(业务逻辑):修改数据
  20. */
  21. actions: {
  22. M_settings(data) {
  23. this.$patch((state) => {
  24. state.settings = { ...state.settings, ...data }
  25. })
  26. },
  27. M_sidebar_opened(data: boolean) {
  28. this.$patch((state) => {
  29. state.sidebar.opened = data
  30. })
  31. },
  32. M_toggleSideBar() {
  33. this.$patch((state) => {
  34. state.sidebar.opened = !state.sidebar.opened
  35. })
  36. },
  37. /*keepAlive缓存*/
  38. M_ADD_CACHED_VIEW(view) {
  39. this.$patch((state) => {
  40. if (state.cachedViews.includes(view)) return
  41. state.cachedViews.push(view)
  42. })
  43. },
  44. M_DEL_CACHED_VIEW(view) {
  45. this.$patch((state) => {
  46. const index = state.cachedViews.indexOf(view)
  47. index > -1 && state.cachedViews.splice(index, 1)
  48. })
  49. },
  50. /*third keepAlive*/
  51. M_ADD_CACHED_VIEW_DEEP(view) {
  52. this.$patch((state) => {
  53. if (state.cachedViewsDeep.includes(view)) return
  54. state.cachedViewsDeep.push(view)
  55. })
  56. },
  57. M_DEL_CACHED_VIEW_DEEP(view) {
  58. this.$patch((state) => {
  59. const index = state.cachedViewsDeep.indexOf(view)
  60. index > -1 && state.cachedViewsDeep.splice(index, 1)
  61. })
  62. },
  63. A_sidebar_opened(data: boolean) {
  64. this.M_sidebar_opened(data)
  65. }
  66. }
  67. })