index.js
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* @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: Pinia 主仓库(全局状态与页面缓存控制)
*/
import { defineStore } from 'pinia';
// import { testStore } from './test'; // 另一个store
// import _ from 'lodash';
import { useRouter } from 'vue-router'
/**
* 主状态仓库
* - auth:授权状态
* - keepPages:用于 keep-alive include 的缓存页面列表
* - appUserInfo:缓存预约人信息(跨页面复用)
* @returns {Function} useStore 方法(调用后获取 store 实例)
*/
export const mainStore = defineStore('main', {
state: () => {
return {
msg: 'Hello world',
count: 0,
auth: false, // 是否已完成授权
keepPages: ['default'], // keep-alive include 为空会全部缓存,这里用默认占位值兜底
appUserInfo: [], // 缓存预约人信息
};
},
getters: {
/**
* 获取缓存页面列表(用于 keep-alive include)
* @returns {string[]} 缓存页面 name 列表
*/
getKeepPages () {
return this.keepPages
},
// getTestStoreList () {
// return testStore().list // 返回另一个store的值
// }
},
actions: {
/**
* 修改授权状态
* @param {boolean} state 授权状态
* @returns {void}
*/
changeState (state) {
this.auth = state;
},
/**
* 清空所有缓存页
* - 用一个不存在的值覆盖,避免 include 为空导致“全页面缓存”
* @returns {void}
*/
changeKeepPages () { // 清空所有缓存,用一个不存在的值覆盖
this.keepPages = ['default'];
},
/**
* 把当前页面加入缓存列表
* - 依赖路由 meta.name 作为 keep-alive include 的 key
* @returns {void}
*/
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)
},
/**
* 缓存预约人信息
* @param {Array<any>} info 预约人信息
* @returns {void}
*/
changeUserInfo (info) {
this.appUserInfo = info;
}
},
});