我有一个名为Users的界面,我正在尝试初始化对象,如下所示:
constructor(private _http: Http) { }
getUsers(): Observable<User[]> {
return this._http.get(this._url)
.map((response: Response) => response.json())
.do(data => console.log("User data" + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(error: Response) {
console.log(error);
return Observable.throw(error.json().error || 'Internal Server error');
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误.
The type argument for type parameter 'T' cannot be inferred from the
usage. Consider specifying the type arguments explicitly. Type
argument candidate 'Response' is not a valid type argument because it
is not a supertype of candidate 'Response'. Types of property 'type'
are incompatible. Type 'string' is not assignable to type 'ResponseType'
Run Code Online (Sandbox Code Playgroud)
URL是JSON文件的文件路径.我试图以这种方式设置它,以便我可以更轻松地转换到真正的HTTP调用,但现在我想使用来自JSON文件的模拟数据.
Response从中导入对象angular2/http
import { Response } from '@angular/http';
Run Code Online (Sandbox Code Playgroud)
您可以阅读或了解更多信息:https: //angular.io/docs/ts/latest/guide/server-communication.html#!#extract-data
| 归档时间: |
|
| 查看次数: |
927 次 |
| 最近记录: |