angular - 在“缓存”时挂起可观察的阻塞路由器
问题描述
我试图在请求时使用我的服务ngrx/effect
来加载数据,但缓存请求以提高效率。我已经阅读了一些示例,它们使用这种方法来存储可观察对象并在存在时再次将其返回,并在publishReplay
refCount
不发送请求的情况下重做请求的响应。
这一切似乎都很完美——但是在第一次请求之后,如果我重用缓存的 observable,Angular
路由器就会停止工作。再次从阅读中,归结为等待可观察的承诺完成?
如何设置服务的消耗以正确解决?
我有一个帮助服务从我的 API 中获取数据,我正在尝试缓存这样的响应:
getResource(params) {
if (!this.cache[cacheKey]) {
this.cache[cacheKey] = this.api.get(RESOURCE_URL, params)
.pipe(
publishReplay(1),
refCount(),
);
}
return this.cache[cacheKey];
}
(api 服务上的 get 方法看起来像这样)
get(resource, params?: object): Observable<ApiResponse> {
return this.http.get<ApiResponse>(resource, params);
}
效果是这样的:
@Effect()
search$: Observable<Action> = this.actions.pipe(
ofType<Search>(Types.SEARCH),
debounceTime(300),
withLatestFrom(this.store.select(state => state.search_properties)),
distinctUntilChanged(),
switchMap(([payload, store]) => {
this.store.dispatch(new GlobalActions.SearchStarted());
const params = merge({include: ['latestPrint']}, store);
return this.lookupService
.getResource(params)
.pipe(
map((response: ApiResponse) => {
this.store.dispatch(new GlobalActions.SearchComplete());
return new ResultActions.SetSearchResults(response);
}),
catchError(() => of(new GlobalActions.EffectError()))
);
})
);
解决方案
推荐阅读
- python - 如何从 csv 文件中读取字符串并将其转换为整数数组?
- mysql - 如果在进行 groupby 之前存在,如何获取值
- javascript - 在检查复选框时,所有其他选票都将以角度检查
- kotlin - 为什么这个 kotlin 代码没有返回最大索引(它总是返回 0 索引)
- r - 如何在 multidplyr 中设置超时
- javascript - html 中的 Controllo 数据数据库 javascript
- python - 如何在 python 中向 Gmail-API 发送批处理请求?
- web - HTTPs - 什么算法用于数据加密/解密
- javascript - 生成包含重复项计数的表格网格(JSON 数据)
- flutter - 如何加载多个短音频并以恒定延迟播放(流畅)