【发布时间】:2017-05-30 19:35:21
【问题描述】:
在我的客户详细信息组件中,我有以下代码可以实现我所追求的目标,但不是以我认为可能的反应/可观察方式。
除了在 if 语句中包装 this.isLoading = true; 之外,有没有办法使用反应式编程技术来做到这一点?如果首先检索到客户,也许通过取消/删除延迟的 observable?或者,我是不是走错了路?
export class CustomerDetailComponent implements OnInit {
customer: Customer;
errorMessage: string;
isLoading: boolean;
constructor(
private customerService: CustomerService,
private route: ActivatedRoute,
private router: Router,
private location: Location
) { }
ngOnInit() {
let idParam = this.route.params
.distinctUntilChanged(params => params['id']);
idParam.subscribe(params =>
{
this.errorMessage = '';
});
idParam.delay(300).subscribe(params =>
{
if (!(this.customer && this.customer.id == params['id']))
this.isLoading = true;
});
idParam.switchMap((params: Params) => this.customerService.getCustomer(params['id']))
.subscribe(customer =>
{
this.customer = customer;
this.isLoading = false;
},
error => this.errorMessage = error);
}
}
【问题讨论】:
标签: angular typescript rxjs reactive-programming rxjs5