Angular RxJs 6链接多个订阅
我有一个角度6应用程序。我正在将rxjs升级到6 在以前版本的应用程序中,我不得不打这样的电话Angular RxJs 6链接多个订阅,angular,rxjs,rxjs6,Angular,Rxjs,Rxjs6,我有一个角度6应用程序。我正在将rxjs升级到6 在以前版本的应用程序中,我不得不打这样的电话 this.service1.getData().concatMap(d1 => { this.d1Local = d1; return this.service2.getData(d1); }).subscribe( this.d2Local = d2, error => err
this.service1.getData().concatMap(d1 => {
this.d1Local = d1;
return this.service2.getData(d1);
}).subscribe(
this.d2Local = d2,
error => errorFunction);
我称之为可观察的
用它的结果做点什么
然后使用上一个变量的值调用另一个可观察变量
用它的结果做点什么
使用concatmap执行此操作。代码看起来像这样
this.service1.getData().concatMap(d1 => {
this.d1Local = d1;
return this.service2.getData(d1);
}).subscribe(
this.d2Local = d2,
error => errorFunction);
我正在尝试使用rxjs6和pipe关键字重写这个,而不使用concatmap
我现在有
this._LocationService.getLocation()
.pipe(
map(location => {
console.log(1);
this.location = location;
return this._GeoCoderService.getAddress(location)
.pipe
(map(address => {
this.address = "You are near " + address;
this.showSpinner = false;
}
))
}
)
);
我从未看到“1”被记录到控制台。
你知道我该怎么做吗
更新: 我知道你有下面的代码。我想这几乎是对的,我没有看到最后的吊唁。日志打印1
this._LocationService.getLocation()
.pipe(map((location: ILocation) => {
this.location = location;
return this._GeoCoderService.getAddress(location);
})
, catchError(error => { this.showSpinner = false; return throwError('Something went wrong!') })
).subscribe(
tap((address: string) => { console.log(1) })
);
我缺少什么?在pipe()
之后,你应该.subscribe()
(可观察对象是惰性的,如果您只是执行pipe()
它不会触发,但subscribe
会触发)在pipe()
之后,您应该.subscribe()
(可观察对象是惰性的,如果您只执行
pipe()
它不会触发,但subscribe
会触发)我不会摆脱concatMap
操作符。它最适合这项工作。为了使事情更干净,我也不会将数据存储在map
操作符中。对于副作用,tap
是可读代码的最佳选择
this.service1.getData().pipe(
tap(d1 => this.d1Local = d1),
concatMap(d1 => this.service2.getData(d1),
).subscribe(
d2 => this.d2Local = d2,
error => errorFunction);
我无法摆脱
concatMap
操作符。它最适合这项工作。为了使事情更干净,我也不会将数据存储在map
操作符中。对于副作用,tap
是可读代码的最佳选择
this.service1.getData().pipe(
tap(d1 => this.d1Local = d1),
concatMap(d1 => this.service2.getData(d1),
).subscribe(
d2 => this.d2Local = d2,
error => errorFunction);
如果
this.\u GeoCoderService.getAddress
返回一个可观察到的值,则需要使用concatMap
(在本例中为mergeMap
)。您还需要在链的末尾订阅。为什么要在subscribe()
中执行tap()
?您不应该在subscribe
中执行tap
-只需subscribe((地址:string)=>console.log(1))
Ifthis.\u GeoCoderService.getAddress
返回您需要使用concatMap
(或在本例中为mergeMap
)的可观察值。您还需要在链的末尾订阅。为什么要在subscribe()
中执行tap()
?您不应该在subscribe
中执行tap
-只需subscribe((地址:string)=>console.log(1))代码>Concatmap已从rxjs6中删除,因此我想将其从代码中删除。不,Concatmap尚未从rxjs6中删除。它仍然在rxjs/operators
中。这是处理高阶观测值的最简单方法。我怀疑这个操作符家族是否会从RxJS中删除。Concatmap已经从rxjs6中删除,所以我想从我的代码中删除它。不,Concatmap还没有从rxjs6中删除。它仍然在rxjs/operators
中。这是处理高阶观测值的最简单方法。我怀疑这个操作符家族是否会从RxJS中移除。