ts封装Storage

385 阅读1分钟
class LocalCaChe {
  store: any
  constructor(islocal: boolean) {
    this.store = islocal ? localStorage : sessionStorage
  }
  setItem(key: string, value: any): void {
    this.store.setItem(key, JSON.stringify(value))
  }
  getItem(key: string): any {
    return JSON.parse(this.store.getItem(key))
  }
  removeItem(key: string): void {
    this.store.removeItem(key)
  }
  clear(): void {
    this.store.clear()
  }
  length(): number {
    return this.setItem.length
  }
  key(index: number): string {
    return this.store.key(index)
  }
}

export default LocalCaChe