store.ts
694 字节
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
enum StoreType {
local = 'local',
session = 'session'
}
class Store {
storage: Storage
constructor(type: StoreType) {
this.storage = type === StoreType.local ? localStorage : sessionStorage
}
set(key: string, value: any) {
if (value) {
this.storage.setItem(key, JSON.stringify(value))
}
}
get(key: string) {
const value = this.storage.getItem(key)
if (value) {
// return JSON.parse(value) || value
return value
}
}
del(key: string) {
this.storage.removeItem(key)
}
clear() {
this.storage.clear()
}
}
const LStore = new Store(StoreType.local)
const SStore = new Store(StoreType.session)
export { LStore, SStore }