mapHelpers.uts
3.91 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Vuex 风格 mapHelpers,兼容旧项目 mapState、mapGetters、mapMutations
* 用法与 vuex 一致,迁移时只需改 import 路径
*
* import { mapState, mapGetters, mapMutations } from '@/store/mapHelpers.uts'
* import store from '@/store/index.uts'
*/
import type { RootState } from "./index.uts";
/** 组件实例类型(含 $store) */
type ComponentThis = {
$store: {
commit: (type: string, payload?: any) => void
dispatch: (name: string, payload?: any) => void
getters: Record<string, any>
state: RootState
}
}
/**
* mapState - 映射 state 到 computed
* mapState(['user']) => { user: () => this.$store.state.user }
* mapState('audio', ['isEnd','isStart']) => { isEnd: () => this.$store.state.audio.isEnd, ... }
* mapState(['point.tabIndex']) => { 'point.tabIndex': () => this.$store.state.point.tabIndex }
*/
export function mapState(map: string[]): Record<string, () => any>
export function mapState(namespace: string, map: string[]): Record<string, () => any>
export function mapState(
namespaceOrMap: string | string[],
map?: string[]
): Record<string, () => any> {
if (typeof namespaceOrMap === "string" && map != null) {
// mapState('audio', ['isEnd','isStart'])
const ns = namespaceOrMap;
const result: Record<string, () => any> = {};
for (const key of map) {
result[key] = function (this: ComponentThis): any {
const moduleState = (this.$store.state as any)[ns];
return moduleState != null ? moduleState[key] : null;
};
}
return result;
}
// mapState(['user','point.tabIndex'])
const keys = namespaceOrMap as string[];
const result: Record<string, () => any> = {};
for (const path of keys) {
const localKey = path.includes(".") ? path : path;
result[localKey] = function (this: ComponentThis): any {
const parts = path.split(".");
let v: any = this.$store.state;
for (const part of parts) {
v = v != null ? v[part] : null;
}
return v;
};
}
return result;
}
/**
* mapGetters - 映射 getters 到 computed
* mapGetters(['customerInfo','receptionStatus'])
*/
export function mapGetters(keys: string[]): Record<string, () => any> {
const result: Record<string, () => any> = {};
for (const key of keys) {
result[key] = function (this: ComponentThis): any {
const g = this.$store.getters as Record<string, any>;
if (g == null || g[key] === undefined) return null;
const v = g[key];
return typeof v === "function" ? v() : v;
};
}
return result;
}
/**
* mapMutations - 映射 mutations 到 methods
* mapMutations('audio', ['setReceptionStatus','setBooks','setPerRecordNumber'])
* mapMutations(['clearCustomerInfo'])
*/
export function mapMutations(map: string[]): Record<string, (payload?: any) => void>
export function mapMutations(
namespace: string,
map: string[]
): Record<string, (payload?: any) => void>
export function mapMutations(
namespaceOrMap: string | string[],
map?: string[]
): Record<string, (payload?: any) => void> {
if (typeof namespaceOrMap === "string" && map != null) {
const ns = namespaceOrMap;
const result: Record<string, (payload?: any) => void> = {};
for (const key of map) {
result[key] = function (this: ComponentThis, payload?: any): void {
this.$store.commit(`${ns}/${key}`, payload);
};
}
return result;
}
const keys = namespaceOrMap as string[];
const result: Record<string, (payload?: any) => void> = {};
for (const key of keys) {
result[key] = function (this: ComponentThis, payload?: any): void {
this.$store.commit(key, payload);
};
}
return result;
}
/**
* mapActions - 映射 actions 到 methods(兼容旧项目若有使用)
* mapActions(['setCustomerInfo','clearHistory'])
*/
export function mapActions(keys: string[]): Record<string, (payload?: any) => void> {
const result: Record<string, (payload?: any) => void> = {};
for (const key of keys) {
result[key] = function (this: ComponentThis, payload?: any): void {
this.$store.dispatch(key, payload);
};
}
return result;
}