mirror of https://github.com/abpframework/abp.git
1 changed files with 25 additions and 8 deletions
@ -1,28 +1,45 @@ |
|||||
import { Injectable } from '@angular/core'; |
import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; |
||||
|
import { isPlatformBrowser } from '@angular/common'; |
||||
|
|
||||
@Injectable({ |
@Injectable({ |
||||
providedIn: 'root', |
providedIn: 'root', |
||||
}) |
}) |
||||
export class AbpLocalStorageService implements Storage { |
export class AbpLocalStorageService implements Storage { |
||||
constructor() {} |
private isBrowser: boolean; |
||||
|
|
||||
|
constructor(@Inject(PLATFORM_ID) private platformId: unknown) { |
||||
|
this.isBrowser = isPlatformBrowser(this.platformId); |
||||
|
} |
||||
[name: string]: any; |
[name: string]: any; |
||||
get length(): number { |
get length(): number { |
||||
return localStorage.length; |
return this.isBrowser ? localStorage.length : 0; |
||||
} |
} |
||||
|
|
||||
clear(): void { |
clear(): void { |
||||
localStorage.clear(); |
if (this.isBrowser) { |
||||
|
localStorage.clear(); |
||||
|
} |
||||
} |
} |
||||
getItem(key: string): string { |
getItem(key: string): string | null { |
||||
|
if (!this.isBrowser) { |
||||
|
return null; |
||||
|
} |
||||
return localStorage.getItem(key); |
return localStorage.getItem(key); |
||||
} |
} |
||||
key(index: number): string { |
key(index: number): string | null { |
||||
|
if (!this.isBrowser) { |
||||
|
return null; |
||||
|
} |
||||
return localStorage.key(index); |
return localStorage.key(index); |
||||
} |
} |
||||
removeItem(key: string): void { |
removeItem(key: string): void { |
||||
localStorage.removeItem(key); |
if (this.isBrowser) { |
||||
|
localStorage.removeItem(key); |
||||
|
} |
||||
} |
} |
||||
setItem(key: string, value: string): void { |
setItem(key: string, value: string): void { |
||||
localStorage.setItem(key, value); |
if (this.isBrowser) { |
||||
|
localStorage.setItem(key, value); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue