使用Observable初始化Angular 2中的接口

时间:2017-03-08 14:31:11

标签: angular

我有一个名为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');
    } 

但是我收到以下错误。

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'

URL是JSON文件的文件路径。我试图以这种方式设置它,以便我可以更轻松地转换到真正的HTTP调用,但现在我想使用来自JSON文件的模拟数据。

1 个答案:

答案 0 :(得分:5)

May be you mean this (main changes marked with **): SELECT Questionnaire_Responses.LoanId AS [Situs ID] ,Loans.MainAccountNumber AS [Account Number] ,Loans.LoanPool ,Exceptions.ExceptionCat AS [Exception Category] ,Questionnaire_Responses.Comments AS [Exception Comments] ,Exceptions.Code AS [Exception Code] ,Exceptions.ExceptionText AS [Exception Description] FROM Questionnaire_Responses INNER JOIN Loans ON Questionnaire_Responses.LoanId = Loans.Id **LEFT JOIN** Exceptions ON Questionnaire_Responses.ExceptionId = Exceptions.Id **LEFT JOIN** Questions ON Questionnaire_Responses.QuestionId = Questions.Id **AND Questions.Active = 1** **LEFT JOIN** Questionnaires ON Questions.QuestionnaireId = Questionnaires.Id **AND Questionnaires.Active = 1** **LEFT JOIN** Loans_Questionnaires_Mapping ON Loans.Id = Loans_Questionnaires_Mapping.LoanId AND Questionnaires.Id = Loans_Questionnaires_Mapping.QuestionnaireId WHERE Loans.Id IN (1, 2, 3, 4) ORDER BY [Situs ID] ,[Exception Description] ,[Exception Code];

导入 Response 对象
angular2/http

您可以阅读或了解更多: https://angular.io/docs/ts/latest/guide/server-communication.html#!#extract-data