mirror of
https://gitee.com/dromara/go-view.git
synced 2026-04-23 00:00:12 +08:00
54 lines
1.0 KiB
TypeScript
54 lines
1.0 KiB
TypeScript
|
|
/**
|
|
* * 存储本地会话数据
|
|
* @param k 键名
|
|
* @param v 键值
|
|
* @returns RemovableRef
|
|
*/
|
|
export const setLocalStorage = <T>(k: string, v: T) => {
|
|
try {
|
|
window.localStorage.setItem(k, JSON.stringify(v))
|
|
} catch (error) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* * 获取本地会话数据
|
|
* @returns any
|
|
*/
|
|
export const getLocalStorage: (k: string) => any = (k: string) => {
|
|
const item = window.localStorage.getItem(k)
|
|
try {
|
|
return item ? JSON.parse(item) : item
|
|
} catch (err) {
|
|
return item
|
|
}
|
|
}
|
|
|
|
/**
|
|
* * 存储临时会话数据
|
|
* @param k 键名
|
|
* @param v 键值
|
|
* @returns RemovableRef
|
|
*/
|
|
export const setSessionStorage = <T>(k: string, v: T) => {
|
|
try {
|
|
window.sessionStorage.setItem(k, JSON.stringify(v))
|
|
} catch (error) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* * 获取临时会话数据
|
|
* @returns any
|
|
*/
|
|
export const getSessionStorage: (k: string) => any = (k: string) => {
|
|
const item = window.sessionStorage.getItem(k)
|
|
try {
|
|
return item ? JSON.parse(item) : item
|
|
} catch (err) {
|
|
return item
|
|
}
|
|
} |