Angular 4将http响应作为接口返回JSON作为属性

时间:2017-08-01 17:19:43

标签: angular typescript

我有一个以前工作的服务,以前只将JSON响应返回到我的组件中(请参阅第二部分以了解以前正在运行的代码)。但是,我正在构建所有组件的附加功能,以便加载动画和错误消息,所以我还需要response.OK和response.status代码。我创建了这个通用响应接口,用于全局变量服务,如下所示:

export interface GetResponse  {
    status: number,
    ok: boolean,
    json: any
}

我在我的服务中使用它如下:

   getInfoDeskTopic(topicUrl) {
        this.infoDeskData = [];
        this.http.get(topicUrl, this.options)
            .map(res => this.response = res)
            .map(res => this.response.json = res.json().resultSet.results)
        this.response.json
                .map(data => data
                    .forEach(item => {
                        let infoDesk = <InfoDesk>{};
                        infoDesk.Url = item.json.webUrl;
                        infoDesk.Date = item.date;
                        item.fields.forEach(item => {
                            if (item.id == 'publication') {
                                infoDesk.Publication = item.strings[0];
                            }
                            if (item.id == 'headline') {
                                infoDesk.Title = item.strings[0];
                            }
                        });
                        this.infoDeskData.push(infoDesk);
                    }))
                .map(data => data = this.infoDeskData)
            .map(items => items.slice(0, 5))
            .share();
        return this.response;
    }

我正试图在我的组件中使用它,如下所示:

this.response = this.infodesk.getInfoDeskTopic(this.topicUrl);
this.news = this.response.json;
this.success = this.response.ok;

我的组件中有变量(this.response:any)。我收到错误:

Uncaught Error: Can't resolve all parameters for InfoDeskService: ([object Object], ?).
    at syntaxError (compiler.es5.js:1540)

能帮助我理解为什么会收到错误吗?我觉得我可能正在宣布错误的东西,但我无法弄清楚问题是什么。感谢

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~

以下是我以前的工作,它工作正常(仅供参考):

在我的服务中:

   getInfoDeskTopic(topicUrl) {
        this.infoDeskData = [];
        return this.http.get(topicUrl, this.options)
            .map(res => res.json().resultSet.results)
                .map(data => data
                    .forEach(item => {
                        let infoDesk = <InfoDesk>{};
                        infoDesk.Url = item.json.webUrl;
                        infoDesk.Date = item.date;
                        item.fields.forEach(item => {
                            if (item.id == 'publication') {
                                infoDesk.Publication = item.strings[0];
                            }
                            if (item.id == 'headline') {
                                infoDesk.Title = item.strings[0];
                            }
                        });
                        this.infoDeskData.push(infoDesk);
                    }))
                .map(data => data = this.infoDeskData)
            .map(items => items.slice(0, 5))
            .share();
    }

在我的组件中:

this.news = this.infodesk.getInfoDeskTopic(this.topicUrl);

1 个答案:

答案 0 :(得分:0)

首先,如果您使用angular 4+,则应使用 HTTPCLIENT HTTP

请搜索差异。.(不需要使用.map)

请查看以下示例:

# A tibble: 10 x 9
   header    ST   adk  fumC  gyrB   icd   mdh  purA  recA
    <int> <int> <int> <int> <int> <int> <int> <int> <int>
 1      1    10    10    11     4     8     8     8     2
 2      2    48     6    11     4     8     8     8     2
 3      3    58     6     4     4    16    24     8    14
 4      4    88     6     4    12     1    20    12     7
 5      5   117    20    45    41    43     5    32     2
 6      6  7036   526     7     1     1     8    71     6
 7      7   101    43    41    15    18    11     7     6
 8      8  3595   112    11     5    12     8    88    86
 9      9   117    20    45    41    43     5    32     2
10     10   744    10    11   135     8     8     8     2