跳至主要內容

localStorage

chanchaw小于 1 分钟react

源码

function set(key: string, val: any): void {
  localStorage.setItem(key, JSON.stringify(val))
}

function get(key: string): any {
  const val = localStorage.getItem(key)
  if (!val) return ''
  try {
    return JSON.parse(val)
  } catch {
    return val
  }
}

function remove(key: string): void {
  localStorage.removeItem(key)
}

function clear(): void {
  localStorage.clear()
}
export { set, get, remove, clear }