import { Injectable } from '@angular/core' ; import { StorageUtil , ExpiredUnit , isBrowser } from './util' ; @ Injectable ( ) export class StorageService { constructor ( private storage ) { } get ( key ) { return StorageUtil . get ( this . storage , key ) ; } set ( key , value , expiredAt = 0 , expiredUnit = 'd' ) { return StorageUtil . set ( this . storage , key , value , expiredAt , expiredUnit ) ; } param key remove ( key ) { if ( typeof key === 'string' ) { StorageUtil . remove ( this . storage , key ) ; return ; } let index = 0 ; let next = StorageUtil . key ( this . storage , index ) ; const ls = [ ] ; while ( next ) { if ( key . test ( next ) ) { ls . push ( next ) ; } next = StorageUtil . key ( this . storage , ++ index ) ; } ls . forEach ( ( v ) => StorageUtil . remove ( this . storage , v ) ) ; } clear ( ) { this . storage ?. clear ( ) ; } } @ Injectable ( { providedIn : 'root' } ) export class LocalStorageService extends StorageService { constructor ( ) { super ( isBrowser ? localStorage : null ) ; } } @ Injectable ( { providedIn : 'root' } ) export class SessionStorageService extends StorageService { constructor ( ) { super ( isBrowser ? sessionStorage : null ) ; } }