import { Injectable, InjectionToken, Inject } from '@angular/core'; export function _sessionStorage() { return sessionStorage; } // #docregion storage-token export const BROWSER_SESSIONSTORAGE = new InjectionToken('Browser SessionStorage', { providedIn: 'root', factory: _sessionStorage }); // #enddocregion storage-token @Injectable() export class NaSessionStorageService { constructor(@Inject(BROWSER_SESSIONSTORAGE) public storage: any) { } public set(key: string, value: string): void { this.storage.setItem(key, value); } public get(key: string): string { return this.storage.getItem(key); } public setObject(key: string, value: any): void { // 将value转变为字符串存储 this.storage.setItem(key, ((value == null) ? null : JSON.stringify(value))); } public getObject(key: string): any { return JSON.parse(this.storage.getItem(key)); } public remove(key: string): any { this.storage.removeItem(key); } }