gpt4 book ai didi

javascript - Angular 2在订阅方法中设置值问题

转载 作者:太空宇宙 更新时间:2023-11-04 15:42:38 26 4
gpt4 key购买 nike

我正在服务器上发表一篇文章,并尝试控制台输出响应http代码。我使用 chrome 工具作为调试器来检查响应,我可以看到输出,但最后它会订阅库执行某些操作并设置函数而不是值。

My component class is

 export class MicroserviceCreateComponent implements OnInit{

errorMessage: string;
postMessage: string;
teams: Team[];
microservice: Microservice[];

constructor(private microserviceService: MicroserviceService) { }

ngOnInit() {
}

onSubmit({ value, valid }: { value: Microservice, valid: boolean }) {
this.createMicroservice(value);
console.log(this.postMessage); <<<<<<<<==== This gives me undefined
}

createMicroservice(value){
this.microserviceService.createMicroservice(value).subscribe(
postMessage => this.postMessage = postMessage.valueOf(),
error => {
this.errorMessage = error.getMessage();
console.error(error.getDescription());
});
}
}

enter image description here

最佳答案

this.microserviceService.createMicroservice(value).subscribe(...) 是一个非阻塞异步调用。这意味着在您调用它之后,可以在 Web 请求完成之前自由打印 console.log(this.postMessage)。尝试将 console.log(this.postMessage) 放入订阅中。

this.createMicroservice(value); // This is an async call
console.log(this.postMessage); // This happens immediately afterwards

关于javascript - Angular 2在订阅方法中设置值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com