我有以下代码
this.subscriptions.push(this.loginservice.userdetails$.valueChanges().subscribe(data => { // subscribing for user data... if any of the logged in user details changed
this.lgUser = data;
this.toFilter.subscribe(filter => { // subject filter value of a dropdown
this.subscriptions.push(this.service.getlist(this.lgUser.oid, filter).subscribe(res => { // subscribing to get list data
// do some operation on res
}));
this.subscriptions.push(this.service.countValue(this.lgUser.oid, filter).subscribe(res => { // subscribing to get count value
this.lCount = res.length;
}));
})
}));
如何使其变得更加突出
谢谢
答案 0 :(得分:1)
好吧,我认为这很干净:
this.loginservice.userdetails$
.valueChanges()
.pipe(
tap(data => this.lgUser = data),
switchMap(data => this.toFilter.pipe(
tap(filter => this.service.countValue(this.lgUser.oid, filter)
.pipe(take(1))
.subscribe(res => this.lCount = res.length)
)),
switchMap(filter => this.service.getlist(this.lgUser.oid, filter))
).subscribe(res => {
// Do something on res
});
this.lgUser
。 toFilter
并使用switchMap
取消先前的待处理请求countValue
即可获取数据并将其存储在lCount
service.getList
并取消以前的待处理请求