Mor*_*n G 29 angular2-services angular
我试图找出如何让菜单显示和消失基于在以前的帖子中登录.但我认为一个更好也可能更容易的问题是,如何观察本地存储的变化?
我在本地存储中使用json web令牌进行身份验证,我很乐意看到对localStorage的更改,然后重新更新我对新信息的看法.
我用这个设置了localStorage
localStorage.setItem('jwt', my_token);
Run Code Online (Sandbox Code Playgroud)
我想做的事情是检查我是否有令牌,如果我什么都没有发生,但是当发生变化时会发生事件.如果我只能观察像localStorage.getItem('jwt')这样的命名事件,我会特别喜欢它.
谢谢!
编辑:
Gunter指出了我正确的方向,但万一有人仍然相当困惑,这里有一个吸引人向你展示如何做到这一点. http://plnkr.co/edit/TiUasGdutCsll1nI6USC?p=preview
Ste*_*hen 30
关键是要使用window.addEventListener("storage",.虽然图书馆可能是角度的"正确"方式,但这是我放在一起的"轻"版本,使用.bind(this)而不是在角度内部进行捣乱.
import { Injectable, OnDestroy } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/share'
@Injectable()
export class StorageService implements OnDestroy {
private onSubject = new Subject<{ key: string, value: any }>();
public changes = this.onSubject.asObservable().share();
constructor() {
this.start();
}
ngOnDestroy() {
this.stop();
}
public getStorage() {
let s = [];
for (let i = 0; i < localStorage.length; i++) {
s.push({
key: localStorage.key(i),
value: JSON.parse(localStorage.getItem(localStorage.key(i)))
});
}
return s;
}
public store(key: string, data: any): void {
localStorage.setItem(key, JSON.stringify(data));
// the local application doesn't seem to catch changes to localStorage...
this.onSubject.next({ key: key, value: data})
}
public clear(key) {
localStorage.removeItem(key);
// the local application doesn't seem to catch changes to localStorage...
this.onSubject.next({ key: key, value: null });
}
private start(): void {
window.addEventListener("storage", this.storageEventListener.bind(this));
}
private storageEventListener(event: StorageEvent) {
if (event.storageArea == localStorage) {
let v;
try { v = JSON.parse(event.newValue); }
catch (e) { v = event.newValue; }
this.onSubject.next({ key: event.key, value: v });
}
}
private stop(): void {
window.removeEventListener("storage", this.storageEventListener.bind(this));
this.onSubject.complete();
}
}
Run Code Online (Sandbox Code Playgroud)
Gün*_*uer 20
使用服务,只能从任何地方通过此服务访问LocalStorage.
然后,该服务可以提供在更改时发出事件的可观察对象,您可以订阅这些可观察对象以获得通知.
jec*_*kep 12
使用新的 rxjs 6更新了/sf/answers/3337816401/ 的版本。
import { Injectable, OnDestroy } from '@angular/core';
import { Subject } from "rxjs";
import { share } from 'rxjs/operators';
@Injectable()
export class StorageService implements OnDestroy {
private onSubject = new Subject<{ key: string, value: any }>();
public changes = this.onSubject.asObservable().pipe(share());
constructor() {
this.start();
}
ngOnDestroy() {
this.stop();
}
public getStorage() {
let s = [];
for (let i = 0; i < localStorage.length; i++) {
s.push({
key: localStorage.key(i),
value: JSON.parse(localStorage.getItem(localStorage.key(i)))
});
}
return s;
}
public store(key: string, data: any): void {
localStorage.setItem(key, JSON.stringify(data));
// the local application doesn't seem to catch changes to localStorage...
this.onSubject.next({key: key, value: data})
}
public clear(key) {
localStorage.removeItem(key);
// the local application doesn't seem to catch changes to localStorage...
this.onSubject.next({key: key, value: null});
}
private start(): void {
window.addEventListener("storage", this.storageEventListener.bind(this));
}
private storageEventListener(event: StorageEvent) {
if (event.storageArea == localStorage) {
let v;
try {
v = JSON.parse(event.newValue);
} catch (e) {
v = event.newValue;
}
this.onSubject.next({key: event.key, value: v});
}
}
private stop(): void {
window.removeEventListener("storage", this.storageEventListener.bind(this));
this.onSubject.complete();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24339 次 |
| 最近记录: |