问题描述
我需要调用/订阅外部服务可观察方法,并从中获取价值。我尝试过,它是组件构造函数,但始终返回未定义。我检查了方法中是否放置了console.log值。需要一些专家帮助才能将该对象->角值的字符串值。
this.x:string;
constructor(
private readonly socService: ocService) { }
ngOnInit(): void {
this.ocService.getActiveoc().subscribe(oc => { this.x= oc.id });
//Todo For Testing
if(this.x){
console.log("ID" + this.x)
this.store.dispatch(selectPatientAction({ x: this.x}));
}
}
onScanX() {
this.store.dispatch(scanX({x: this.x }));
this.isocSelected = true;
}
解决方法
要使用该值进行的所有操作都应在subscribe
方法中完成。也可以使用该方法调度您的操作。
我假设您在这里使用NgRedux。
constructor(
private readonly socService: ocService
private store: NgRedux<AppState>
) { }
ngOnInit(): void {
this.ocService.getActiveoc().subscribe(oc => {
this.x = oc.id;
this.store.dispatch(selectPatientAction({ x: this.x}));
});
}
onScanX() {
this.store.dispatch(scanX({x: this.x }));
this.isocSelected = true;
}
,
我认为您在本节中有错字?
this.ocService.getActiveoc().subscribe(soc => { this.x= oc.id });
我认为应该是soc.id
而不是oc.id
?