我是angular的初学者。我希望有人能帮助我。我用下面的代码在Angularframework中调用一个REST接口:
public getAntwort(){
return this.http.get<Mail>('http://......')
.pipe(retry(3),catchError(this.handleError));
}
在调用组件中,我将其保存在一个用any.
定义的字段中。export class SemailUiComponent implements OnInit {
mail: any;
constructor(private semailDataService: SemailService) {
}
ngOnInit(): void {
this.mail = this.semailDataService.getAntwort();
}
}
如果我输出this。邮件字段,我得到的不是字符串,而是对象。接口的其余部分传递一个字符串。第二个问题是,我想打印字符串。如何在HTML中做到这一点?谢谢你的回答
this.http.method
返回一个Observable。您需要subscribe
以获取数据并实际执行http请求。
你的代码只是缺少了这一部分:
export class SemailUiComponent implements OnInit {
mail: any;
constructor(private semailDataService: SemailService) {
}
ngOnInit(): void {
this.semailDataService.getAntwort().subscribe(res => {
this.mail = res;
});
}
}
提示:如果您试图在编辑您的代码中,你会看到url没有发出请求:
'http://......'
.