Browse Source

fix: storage check for the local storage service

pull/25729/head
sumeyye 3 months ago
parent
commit
ba40a4a624
  1. 37
      npm/ng-packs/packages/core/src/lib/services/local-storage.service.ts

37
npm/ng-packs/packages/core/src/lib/services/local-storage.service.ts

@ -10,35 +10,36 @@ export class AbpLocalStorageService implements Storage {
constructor() { constructor() {
} }
[name: string]: any; [name: string]: any;
private get storage(): Storage | null {
if (!isPlatformBrowser(this.platformId) || typeof window === 'undefined') {
return null;
}
try {
return window.localStorage;
} catch {
return null;
}
}
get length(): number { get length(): number {
return isPlatformBrowser(this.platformId) ? localStorage.length : 0; return this.storage?.length || 0;
} }
clear(): void { clear(): void {
if (isPlatformBrowser(this.platformId)) { this.storage?.clear();
localStorage.clear();
}
} }
getItem(key: string): string | null { getItem(key: string): string | null {
if (!isPlatformBrowser(this.platformId)) { return this.storage?.getItem(key) || null;
return null;
}
return localStorage.getItem(key);
} }
key(index: number): string | null { key(index: number): string | null {
if (!isPlatformBrowser(this.platformId)) { return this.storage?.key(index) || null;
return null;
}
return localStorage.key(index);
} }
removeItem(key: string): void { removeItem(key: string): void {
if (isPlatformBrowser(this.platformId)) { this.storage?.removeItem(key);
localStorage.removeItem(key);
}
} }
setItem(key: string, value: string): void { setItem(key: string, value: string): void {
if (isPlatformBrowser(this.platformId)) { this.storage?.setItem(key, value);
localStorage.setItem(key, value);
}
} }
} }

Loading…
Cancel
Save