在AnguylarJS中,我会在一个服务中做类似的事情:
function call(){
if(service.promise){
return service.promise
};
service.promise = http.get(...)
.then(function(){
...
return ...;
}, function(){
...
});
return service.promise;
}
call(); //fires the http call
call(); //does not fire the http call again
我尝试用角度上的观察值来复制它,但是HTTP调用总是为每个订阅触发:
call(): Observable<...>{
if(service.observable){
return service.observable;
}
service.observable = this.httpClient.get<...>(...)
.flatMap(data => {
...
return of(...);
});
return service.observable;
}
call() //fires the http call
.subscribe((data) => {
console.log(data);
})
call() //fires the http call again
.subscribe((data) => {
console.log(data);
})
我也可以在这里做反模式。