Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 让拦截器读取缓存值_Angular_Ionic Framework - Fatal编程技术网

Angular 让拦截器读取缓存值

Angular 让拦截器读取缓存值,angular,ionic-framework,Angular,Ionic Framework,我试图让我的拦截器从缓存中读取,以查看在脱机时是否已经缓存了post请求。如果是这样,我将返回缓存的本地数据并显示它。缓存正在使用离子存储缓存 在本例中,我只检查了一个url模式(api/dfrs/id),并缓存了该值。如果设置了某个键,我将在缓存中循环并获取值。每个缓存值都是post请求本身 最终会找到该值,但由于获取该值的方法是异步的,因此在返回缓存结果之前,拦截器代码将继续运行 所以我想解决两个问题 1) 让第一个if语句中的代码在该promise方法上运行 2) 将缓存请求的主体作为可观

我试图让我的拦截器从缓存中读取,以查看在脱机时是否已经缓存了post请求。如果是这样,我将返回缓存的本地数据并显示它。缓存正在使用离子存储缓存

在本例中,我只检查了一个url模式(api/dfrs/id),并缓存了该值。如果设置了某个键,我将在缓存中循环并获取值。每个缓存值都是post请求本身

最终会找到该值,但由于获取该值的方法是异步的,因此在返回缓存结果之前,拦截器代码将继续运行

所以我想解决两个问题

1) 让第一个if语句中的代码在该promise方法上运行

2) 将缓存请求的主体作为可观察对象返回,以便我的视图可以使用它

任何帮助都将不胜感激。谢谢

令牌拦截器

`

`

//dfr.service.ts(仅类中的方法) `

`

//view.ts(仅限订阅) `

`


我希望我的缓存请求的主体被返回

LOL终于得到了它。我无法倾听承诺,然后不得不返回一个可观察到的。希望这能帮助一些人;0

替换此代码

return next.handle(request).pipe (
    map((event: any) => {
      let val = this.getCachedResult(request);
      return (Observable.create(val));
    })
  )


好吧,我通过使用from和switchMap解决了第一部分,因为我可以看到在承诺完成后,调用正在完成。例如:类似于从(this.getCachedResult(request)).pipe(switchMap(data=>{return next.handle(Observable.create(data))//return next.handle(request)})返回的内容,现在我只需要将缓存请求中的数据作为可观察数据返回
import { Injectable } from '@angular/core';
import { Storage, IonicStorageModule } from '@ionic/storage';
import { Network } from '@capacitor/core';
import { HttpClient } from '@angular/common/http';
import { SharedService } from '../shared/shared.service';
import { DFRPhotoService } from '../dfr/daily-summary/dfr-photo/dfr-photo.service';
import { LoadingController } from '@ionic/angular';

@Injectable({
  providedIn: 'root'
})
export class CacheSaveDataService {

  status = null;
  handler;
  loaderElement;

  constructor(
      public storage: Storage,
      private httpClient: HttpClient,
      private sharedService: SharedService,
      private loadingController: LoadingController) {

    this.status = this.getStatus();
    if (this.handler === undefined) {
      console.log('Add Listener ', status);
      this.handler = Network.addListener('networkStatusChange', (status) => {
        console.log('Network status changed! ', status);
        const oldStatus = this.status;
        this.status = status;
        if (this.status.connected && !oldStatus.connected) {
          this.saveCachedData();
        }
        this.status = status;
        if (!this.status.connected) {
          this.sharedService.showNotification('Network connection has terminated.', 'danger', 5000);
        }
      });
    }
  }



  public async setItem(key, value) {
    return await this.storage.set(key, value);
  }

  public async setStorageRequest2(request) {
    const cacheKey = 'reqcache-' + this.sharedService.generateId();
    return await this.storage.set(cacheKey, request);
  }

  public async setStorageRequest(request) {
    this.sharedService.showNotification('User is offline. Service Request Cached', 'warning', 2000);
    const cacheKey = 'reqcache-' + this.sharedService.generateId() + '-' + request.urlWithParams;
    return await this.storage.set(cacheKey, request);
  }

  public async get(settingName){
    return await this.storage.get(`setting:${ settingName }`);
  }

  public async get2(settingName) {
    return await this.storage.get(settingName);
  }

  public async remove(settingName){
    return await this.storage.remove(settingName);
  }

  public clear() {
    this.storage.clear().then(() => {
      console.log('all keys cleared');
    });
  }

  public async getKeys() {
    return await this.storage.keys();
  }

  public async getStatus() {
    this.status = await Network.getStatus();
  }


}
public getDFRByDfrId(id: string) {
    return this.httpClient.get(environment.apiURL + this.path + '/id/' + id)
    .pipe(
      map((res: any) => {
        return res.data;
      })
    );
  }
   this.dfrService.getDFRByDfrId(paramMap.get('id')).subscribe(val => {
               this.currentDFR = val;
            });
return next.handle(request).pipe (
    map((event: any) => {
      let val = this.getCachedResult(request);
      return (Observable.create(val));
    })
  )
return from(this.getCachedResult(request)).pipe (
      mergeMap(data => {
         return of(new HttpResponse({ body: data.body }))
      })
    )