如何使用 NgZone 修复 chrome 存储检索延迟?

问题描述

我有一个 Angular 组件,需要在 NgOnInit 方法中设置和获取数据。

但是,由于时间原因,数据设置和检索会稍有延迟。我想用 ngzoneChangeDetectionRef 解决这个问题,但我不确定如何去做。

我不想使用设置的间隔或时间延迟。

我在 constructor调用加载数据方法

constructor(
   readonly chromeStorageService: ChromeStorageService) {
       this.chromeStorageService.loadData(this.chromeStorageKey);
}

在我的服务方法中,如果尚未在 Chrome 存储中设置数据,我会加载数据。

loadData(key: string) {
    chrome.storage.local.get(
         [key],async (result: {[key: string]: VersionStorage;}) => {
              if (Object.keys(result).length === 0 &&
                  result.constructor ===
                      Object) {  // check to see if object is there
                const todaysDate = new Date().getTime();
                const object = {timestamp: todaysDate,versionNumber: '3.0.9'};
                chrome.storage.local.set({key: object});
              }
    });
}

然后我在我的 OnInit 方法中检索数据:

async ngOnInit() {
   this.chromeStorageService.retrieveChromeStorageObject(
       this.chromeStorageKey);
    this.chromeStorageService.chromeStorageSubject
            .pipe(takeuntil(this.ngDestroy),tap((chromeObject: VersionStorage) => {
                    this.shoulddisplay = this.isNewVersion(chromeObject);
                  }))
            .subscribe();
}

但是延迟导致我的对象为空。有人能用 ngzoneChangeDetectionRef 帮我解决这个问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)