Angular Rxjs init资源一次,热可观测
我有以下代码的服务设置:Angular Rxjs init资源一次,热可观测,angular,typescript,rxjs,observable,system.reactive,Angular,Typescript,Rxjs,Observable,System.reactive,我有以下代码的服务设置: @Injectable() export class StaticResourcesService { private resourcesTree$: Observable<any>; getSpecificLabel(resourceName: string, type: string): Observable<string> { return this.getResources().map(resources => {
@Injectable()
export class StaticResourcesService {
private resourcesTree$: Observable<any>;
getSpecificLabel(resourceName: string, type: string): Observable<string> {
return this.getResources().map(resources => {
return jp.value(resources, `$.${resourceName}[?(@.type=="${type}")].label`);
});
}
private getResources(): Observable<any> {
if (this.resourcesTree$ != null) {
return this.resourcesTree$;
}
let source = this.getResourcesFilePath() // get obs<string> with a file path
.mergeMap(path => {
console.log(`static-resources path: ${path}`);
return this.http.get(path).map(res => res.json()); // get json obj
}).do(res => {
this.resourcesTree$ = Observable.of(res);
});
return source;
}
具有以下变换方法的自定义角度管道:
transform(resourceType: string, resourceName: string): Observable<string> {
return this.staticResourcesService.getSpecificLabel(resourceName, resourceType);
}
我在一个简单的html页面中使用了这个自定义管道:
<div *ngFor="let item of items">{{ item?.status | staticResource:'MY_RES' | async }}</div>
当我第一次加载页面时,自定义管道将处理4次ngFor循环4x,并且永远不会从resourcesTree$获取其源数据。我理解这是一种正常行为,因为订阅将同时侦听,并且do将执行4次。但我想要的是一种优化的方式,只访问一次数据,然后在接下来的3次中利用resourcesTree$。我试图通过在源代码上使用share方法使它变热。但我不能让它正常工作
有人能告诉我如何做到这一点吗?也许我完全错了,我真的愿意接受这里的任何建议
谢谢异步服务的通常模式是将共享资源作为主题或ReplaySubject。这样,你只需连接订户,他们就可以很好地等待数据的到来 在您的示例中,它对资源进行切片,因此这是一个映射操作符,正如您已经做过的那样。还有一种表示提取已开始的方法,不必在意提取是否已完成,因为它是可观察的 由于getResources不再在外部订阅,因此需要将.do替换为.subscribe
@Injectable()
export class StaticResourcesService {
private resourcesTree$ = new ReplaySubject(1);
private status = 'unloaded';
getSpecificLabel(resourceName: string, type: string): Observable<string> {
if( this.status === 'unloaded') {
this.status = 'loading';
getResources();
}
return resourcesTree$.map(resources => {
return jp.value(resources, `$.${resourceName}[?(@.type=="${type}")].label`);
});
}
private getResources(): Observable<any> {
this.getResourcesFilePath() // get obs<string> with a file path
.mergeMap(path => {
console.log(`static-resources path: ${path}`);
return this.http.get(path).map(res => res.json()); // get json obj
})
.subscribe(res => {
this.resourcesTree$.next(res);
this.status = 'loaded';
});
}
你确定这是可能的吗?因为在getSpecificLabel中,您基本上创建了4个完全不同的观测值。分享对你没有帮助。我认为这可以通过制作“map observable”管道来实现,但这取决于jp.value的作用。jp.value在这里不是很相关,它只帮助我使用jsonpathYour observable将特定标签检索到我的json对象中。您的observable有副作用-这可能表明您做错了什么。我认为使用这种方法是不可能的。你能描述一下你到底想达到什么目的吗?它应该根据item.status字段显示URL中的JSON文件?是的,我有一个JSON对象:任务类型:[{TYPE:PRODUCT\u DELIVERY,label:livraisson produit},{TYPE:MANUAL\u PRODUCT\u DELIVERY,label:livraisson produit manuelle},{TYPE:OTHER\u DELIVERY,label:livraisson autre}]我想得到每个代码的标签。但是我只需要对json文件进行一次访问,然后使用我的resourcesTree$变量从内存中获取值。但是,当JSON在过去已经下载时,是否使用缓存值?