我对rxjs非常陌生,只是想知道是否可以通过管道和tap来设置类属性,还是应该在订阅中进行。 对我来说,任何一种方式都可以,只是想知道是否可以按照我的意愿进行操作,或者是否有我不知道的问题。
演示两种方式的TypeScript代码:
export class ViewComponent implements OnInit {
applicant = {};
constructor(public route: ActivatedRoute, private store: Store<any>) {}
ngOnInit() {
this.route.paramMap.pipe(
switchMap(params => this.store.select(state => state.applicants.entities[params.get('id')])),
tap(applicant => this.applicant = applicant)
).subscribe();
}
}
对比
export class ViewComponent implements OnInit {
applicant = {};
constructor(public route: ActivatedRoute, private store: Store<any>) {}
ngOnInit() {
this.route.paramMap.pipe(
switchMap(params => this.store.select(state => state.applicants.entities[params.get('id')]))
).subscribe(applicant => this.applicant = applicant);
}
}
tap
做的事情都可以在subscribe
中完成,因此在任何地方使用tap
可能会是“实际上不需要的更多代码”。 - vince