Angular RxJS flatMap this _this
我正在尝试使2链订阅一个flatMap,但是flatMap中的" this "不是指我的组件,而是指MergeMapSubscriber。在运行时_this引用我的组件,但是如果我在代码中使用它,TypeScript不会编译
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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) ); |
完整代码...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | @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); } } |
其他详细信息,因此堆栈溢出将接受post
要访问组件的
在新版本的rxjs中,该运算符称为mergeMap。
https://www.learnrxjs.io/operators/transformation/mergemap.html