【发布时间】:2021-09-11 19:01:57
【问题描述】:
在我的角度服务中,我有一个缓存的 http 请求,在我的组件初始化时调用一次
//variables
private readonly URL: string = 'http://makeup-api.herokuapp.com/api/v1/products.json';
private cachingSubject: ReplaySubject<IProduct[]>
private _products: IProduct[] = [];
//method
public getAllProducts(): Observable<IProduct[]> {
if (!this.cachingSubject) {
this.cachingSubject = new ReplaySubject<IProduct[]>(1);
this.http.get<IProduct[]>(this.URL)
.subscribe((productsData: IProduct[]) => {
this._products = productsData;
this.cachingSubject.next(productsData);
});
}
return this.cachingSubject.asObservable();
}
和我的组件在创建服务方法时:
public products: IProduct[] = [];
private destroySubject$: Subject<void> = new Subject<void>();
ngOnInit(): void {
this.productService
.getAllProducts()
.pipe(takeUntil(this.destroySubject$))
.subscribe((products: IProduct[]) => {
this.products = products;
})
}
public onForceReload(): void {
//need to reload onClick
}
问题是,如何从我的缓存请求中强制重新加载(重新初始化缓存)?再次制作 getAllProducts
【问题讨论】:
标签: angular caching rxjs observable subject