goview_vue/src/utils/storage.ts

55 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-12-20 13:36:54 +08:00
/**
* *
* @param k
2022-03-06 02:08:14 +08:00
* @param v stringiiy
2021-12-20 13:36:54 +08:00
* @returns RemovableRef
*/
export const setLocalStorage = <T>(k: string, v: T) => {
try {
window.localStorage.setItem(k, JSON.stringify(v))
} catch (error) {
return false
}
}
/**
* *
2022-03-06 02:08:14 +08:00
* @param k
2021-12-20 13:36:54 +08:00
* @returns any
*/
2022-03-06 02:08:14 +08:00
export const getLocalStorage = (k: string) => {
2021-12-20 13:36:54 +08:00
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
}
}