ذخیره سازی اطلاعات در مرورگر توسط برنامههای Angular
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۰۸/۱۶ ۱۴:۴۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
import { Injectable } from "@angular/core";
@Injectable()
export class BrowserStorageService {
getSession(key: string): any {
const data = window.sessionStorage.getItem(key);
return JSON.parse(data);
}
setSession(key: string, value: any): void {
const data = value === undefined ? null : JSON.stringify(value);
window.sessionStorage.setItem(key, data);
}
removeSession(key: string): void {
window.sessionStorage.removeItem(key);
}
removeAllSessions(): void {
for (const key in window.sessionStorage) {
if (window.sessionStorage.hasOwnProperty(key)) {
this.removeSession(key);
}
}
}
getLocal(key: string): any {
const data = window.localStorage.getItem(key);
return JSON.parse(data);
}
setLocal(key: string, value: any): void {
const data = value === undefined ? null : JSON.stringify(value);
window.localStorage.setItem(key, data);
}
removeLocal(key: string): void {
window.localStorage.removeItem(key);
}
removeAllLocals(): void {
for (const key in window.localStorage) {
if (window.localStorage.hasOwnProperty(key)) {
this.removeLocal(key);
}
}
}
} import { BrowserStorageService } from "./browser-storage.service";
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { RouterModule } from "@angular/router";
@NgModule({
imports: [CommonModule, RouterModule],
exports: [], // components that are used in app.component.ts will be listed here.
declarations: [], // components that are used in app.component.ts will be listed here.
providers: [BrowserStorageService] // singleton services of the whole app will be listed here.
})
export class CoreModule { }; import { CoreModule } from "./core/core.module";
@NgModule({
imports: [
//...
CoreModule,
//...
RouterModule.forRoot(appRoutes)
],
//...
})
export class AppModule { } import { BrowserStorageService } from "./../../core/browser-storage.service";
import { Component, OnInit } from "@angular/core";
@Component({
selector: "app-browser-storage-sample-test",
templateUrl: "./browser-storage-sample-test.component.html",
styleUrls: ["./browser-storage-sample-test.component.css"]
})
export class BrowserStorageSampleTestComponent implements OnInit {
fromSessionStorage = "";
fromLocalStorage = ""
sessionStorageKey = "sessionStorageKey1";
localStorageKey = "localStorageKey1"
constructor(private browserStorage: BrowserStorageService) { }
ngOnInit() {
}
sessionStorageSetItem() {
this.browserStorage.setSession(this.sessionStorageKey, "Val1");
}
sessionStorageGetItem() {
this.fromSessionStorage = this.browserStorage.getSession(this.sessionStorageKey);
}
localStorageSetItem() {
this.browserStorage.setLocal(this.localStorageKey, { key1: "val1", key2: 2 });
}
localStorageGetItem() {
this.fromLocalStorage = JSON.stringify(this.browserStorage.getLocal(this.localStorageKey));
}
} <h1>Browser storage sample</h1>
<div class="panel">
<button class="btn btn-primary" (click)="sessionStorageSetItem()" type="button">sessionStorage -> Set Item</button>
<button class="btn btn-success" (click)="sessionStorageGetItem()" type="button">sessionStorage -> Get Item</button>
<div class="alert alert-info" *ngIf="fromSessionStorage">
{{fromSessionStorage}}
</div>
</div>
<div class="panel">
<button class="btn btn-warning" (click)="localStorageSetItem()" type="button">localStorage -> Set Item</button>
<button class="btn btn-success" (click)="localStorageGetItem()" type="button">localStorage -> Get Item</button>
<div class="alert alert-info" *ngIf="fromLocalStorage">
{{fromLocalStorage}}
</div>
</div>