我试图弄清楚如何根据以前的帖子登录来显示和消失菜单。但我认为一个更好也可能更容易的问题是,我如何关注本地存储的变化?
我在本地存储中使用json网络令牌进行身份验证,我很乐意看到对localStorage的更改,然后重新更新我对新信息的看法。
我用这个
设置了localStorageHouse
我想做的事情是检查我是否有令牌,如果我什么都没有发生,但是当发生变化时会发生事件。如果我只能观察像localStorage.getItem('jwt')这样的命名事件,我会特别喜欢它。
谢谢!
修改
Gunter指出我正确的方向,但万一有人仍然相当困惑,这里是一个让你知道怎么做的傻瓜。 http://plnkr.co/edit/TiUasGdutCsll1nI6USC?p=preview
答案 0 :(得分:23)
关键是使用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();
}
}
答案 1 :(得分:19)
使用服务,只能从任何地方通过此服务访问LocalStorage 然后,该服务可以提供在更改时发出事件的可观察对象,您可以订阅这些可观察对象以获得通知。
答案 2 :(得分:1)
我知道这篇文章有点陈旧但有些库可以为你做这件事。例如,h5webstorage将允许您像处理常规对象一样处理localStorage和sessionStorage,并自动处理同步。即使对存储的直接更改也会重新流回您的应用。
答案 3 :(得分:1)
使用新的rxjs 6更新了https://stackoverflow.com/a/47683091/3098882的版本。
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();
}
}