我正在尝试进行 http 调用以从 Angular 中的远程服务器获取一些数据。我正在使用 Angular 6。在这里,我可以使用下面提到的两种方法 (getEmploy, getEmploy1
) 获取记录列表。
问题是我不明白这两种调用 API URL 的方式有什么区别,我应该使用哪种方法。
服务.ts:
<pre>
getEmploy() : Observable<IEmployee[]>
{
return this.http.get<IEmployee[]>(this._url);
}
getEmploy1()
{
return this.http.get(this._url);
}
</pre>
最佳答案
在方法 1 中,您要添加一个类型
IEmployee
到您从 API 接收的数据,所以您说数据是 IEmployee 类型。如果您真的想利用 TypeScript,这就是您应该做的。
在方法二中,数据的类型变成了
any
.. 因为它是任何.. 你真的没有得到打字的好处。
关于javascript - 如何理解 Angular 服务调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53635206/