index.js 1.41 KB
/*
 * @Date: 2022-04-18 15:59:42
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2024-01-30 15:26:30
 * @FilePath: /xysBooking/src/store/index.js
 * @Description: 文件描述
 */
import { defineStore } from 'pinia';
// import { testStore } from './test'; // 另一个store
// import _ from 'lodash';
import { useRouter } from 'vue-router'

export const mainStore = defineStore('main', {
  state: () => {
    return {
      msg: 'Hello world',
      count: 0,
      auth: false,
      keepPages: ['default'], // 很坑爹,空值全部都缓存
      appUserInfo: [], // 缓存预约人信息
    };
  },
  getters: {
    getKeepPages () {
      return this.keepPages
    },
    // getTestStoreList () {
    //   return testStore().list // 返回另一个store的值
    // }
  },
  actions: {
    changeState (state) {
      this.auth = state;
    },
    changeKeepPages () { // 清空所有缓存,用一个不存在的值覆盖
      this.keepPages = ['default'];
    },
    keepThisPage () { // 新增缓存页
      const $router = useRouter();
      const page = $router.currentRoute.value.meta.name;
      this.keepPages.push(page);
    },
    removeThisPage () { // 删除缓存页
      // const $router = useRouter();
      // const page = $router.currentRoute.value.meta.name;
      // _.remove(this.keepPages, item => item === page)
    },
    changeUserInfo (info) {
      this.appUserInfo = info;
    }
  },
});