简体   繁体   English

Angular - 如何始终在 localStorage 获取最新数据?

[英]Angular - How to always get the newest data at localStorage?

I know that there is a lot of questions on SO talking about this issue, but until now I didn't find any topic that could solve this problem.我知道有很多关于 SO 谈论这个问题的问题,但直到现在我没有找到任何可以解决这个问题的话题。 Basically, I have a service responsible for get data from service and save locally and a component to show this data.基本上,我有一个服务负责从服务中获取数据并保存在本地,还有一个组件来显示这些数据。 But every time I send a new request the component show the old data instead of the new.但是每次我发送新请求时,组件都会显示旧数据而不是新数据。 I already tried to:我已经尝试过:

localStorage.removeItem(this.dataToShow);
localStorage.setItem(this.dataToShow, JSON.stringify(content));

But even this solve this issue.但即使这样也解决了这个问题。

Well you need the component data updated by the time the local storage value updated.那么您需要在本地存储值更新时更新组件数据。 Ya ?是吗?

In Angular there is no binding to local storage.在 Angular 中,没有绑定到本地存储。 In other words you have to get item value from local storage every time it is updated.换句话说,每次更新时,您都必须从本地存储中获取项目值。

So I suggest you to import the service and subscribe to a subject so any change can be propagate to component.所以我建议你导入服务并订阅一个主题,这样任何更改都可以传播到组件。

so In your service call Subject.next() to send message to observable.所以在你的服务中调用 Subject.next() 来发送消息给 observable。 so the subscribe will be notified.所以订阅者会收到通知。

Here is a help full link.这是一个帮助完整链接。 http://jasonwatmore.com/post/2016/12/01/angular-2-communicating-between-components-with-observable-subject http://jasonwatmore.com/post/2016/12/01/angular-2-communicating-between-components-with-observable-subject

Well from what i could understand you want to show the data stored locally first you need to save the data locally like that localStorage.setItem('mean-token', token);好吧,据我所知,您想首先显示本地存储的数据,您需要将数据保存在本地,如 localStorage.setItem('mean-token', token); mean-token is the name and token is the object and you can call the object by the name : const name = localStorage.getItem('mean-token'); mean-token 是名称,token 是对象,您可以通过名称调用对象: const name = localStorage.getItem('mean-token'); and to remove it you can use : localStorage.removeItem('mean-token') hope thats work if not can u explain the probleme better并删除它,你可以使用:localStorage.removeItem('mean-token') 希望那是有效的,如果不是你能更好地解释问题吗

I'm using ngx-webstorage-service我正在使用ngx-webstorage-service

You'll need to initialize an Observable method which returns a Subject variable to track changes in your Local Storage service.您需要初始化一个Observable方法,该方法返回一个Subject变量以跟踪本地存储服务中的更改。

import { Observable, Subject } from 'rxjs';
import { LOCAL_STORAGE, StorageService } from 'ngx-webstorage-service';
import { Inject, Injectable } from '@angular/core';

// Key used to access status in local storage
const STATUS_STORAGE_KEY = 'status_test';

@Injectable()
export class LocalStorageService {
  private storageStatus = new Subject<string>();
  constructor (
    @Inject(LOCAL_STORAGE)
    private storage: StorageService)
  {}

  watchStorage(): Observable<any> {
    return this.storageStatus.asObservable();
  }

  // Set Method
  public setStatus(status: boolean) {
    this.storage.set(STATUS_STORAGE_KEY, status);
    this.storageStatus.next('changed'); // tell subscribers storage status is updated
  }

  // Get Method
  public getStatus() {
    return this.storage.get(STATUS_STORAGE_KEY);
  }
}

In your component.ts在你的 component.ts

Setting new data into local storage will trigger the Behavior variable and subscribers will then be notified that changes are made将新数据设置到本地存储将触发Behavior变量,然后将通知订阅者进行了更改

this.localStorage.setStatus(true);

Subscribe to the observable so that whenever the storage has new data added in, it will be updated as well.订阅 observable 以便每当存储添加新数据时,它也会更新。

this.localStorage.watchStorage().subscribe(() => {
  this.updatedStatus = this.localStorage.getStatus();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM