如何从打字稿中的异步方法中获取价值?
How take value from async method in typescript?
我需要 call/subscribe 到外部服务可观察方法并从中获取值。我试了一下,它是组件构造器,但总是 return 未定义。我检查了方法中的 put console.log 值在那里。需要一些专家帮助来获取此对象 -> angular.
中的字符串值
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
?
我需要 call/subscribe 到外部服务可观察方法并从中获取值。我试了一下,它是组件构造器,但总是 return 未定义。我检查了方法中的 put console.log 值在那里。需要一些专家帮助来获取此对象 -> angular.
中的字符串值 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
?