[英]Angular RxJS flatMap this _this
I am trying to chain 2 subscribes with a flatMap but the "this" inside the flatMap does not refer to my component but to MergeMapSubscriber. 我正在尝试使2订阅一个flatMap,但是flatMap中的“ this”不是指我的组件,而是指MergeMapSubscriber。 At runtime _this refers to my component but if I use that in my code TypeScript does not compile
在运行时_this指向我的组件,但是如果我在代码中使用它,TypeScript不会编译
import { flatMap } from 'rxjs/operators';
this.payexMasterService.queryOk().flatMap(
(res: ResponseWrapper) => {
_this.templateData.dataSets[0].data[0] = res.json;
return _this.payexMasterService.queryError();
}
).subscribe(
(res: ResponseWrapper) => {
this.templateData.dataSets[0].data[1] = res.json;
this.data = Object.assign({}, this.templateData);
},
(res: ResponseWrapper) => this.onError(res)
);
the full code ... 完整的代码...
@Component({
selector: 'pay-pie',
templateUrl: './payex-master-pie.component.html',
styleUrls: []
})
export class PayexMasterPieComponent implements OnInit {
numOk: number;
numError: number;
currentAccount: any;
eventSubscriber: Subscription;
data: any;
options: any;
templateData: any;
constructor(
private payexMasterService: PayexMasterService,
private jhiAlertService: JhiAlertService,
private eventManager: JhiEventManager,
private principal: Principal
) {
this.templateData = {
labels: ['OK', 'Error'],
datasets: [{
data: [0, 0],
backgroundColor: [
'#36A2EB',
'#FF6384',
],
hoverBackgroundColor: [
'#36A2EB',
'#FF6384',
]
}]
};
}
loadAll() {
this.payexMasterService.queryOk().pipe(flatMap(
(res: ResponseWrapper) => {
this.templateData.dataSets[0].data[0] = res.json;
return this.payexMasterService.queryError();
}
)).subscribe(
(res: ResponseWrapper) => {
this.templateData.dataSets[0].data[1] = res.json;
this.data = Object.assign({}, this.templateData);
},
(res: ResponseWrapper) => this.onError(res)
);
}
ngOnInit() {
this.loadAll();
this.principal.identity().then((account) => {
this.currentAccount = account;
});
}
private onError(error) {
this.jhiAlertService.error(error.message, null, null);
}
}
extra details so stack overflow will accept post 额外的细节,以便堆栈溢出将接受发布
To access to the this
of your component, you should use the pipe function. 要访问组件的
this
,您应该使用管道功能。 .pipe(flatMap(...))
. .pipe(flatMap(...))
The operator is called mergeMap in the new version of rxjs. 在新版本的rxjs中,该运算符称为mergeMap。 https://www.learnrxjs.io/operators/transformation/mergemap.html
https://www.learnrxjs.io/operators/transformation/mergemap.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.