HTTP Request Angular 2

时间:2016-11-04 14:47:41

标签: json http angular get angular2-services

我尝试在角度2中从外部API发出请求。 我想在2个文件中管理数据请求,并将结果显示为json。

我的数据输出组件如下所示:

import {Component} from '@angular/core'
import {DataService} from './datavisualisation.service'

    @Component({
        selector: 'app-datavisualisation-output',
        template: ` 
      `
    })
    export class DatavisualisationOutput {
        constructor(dataservice: DataService) {
            dataservice.data
                .subscribe(
                    data => this.data = data,
                    console.error,
                    () => console.log('Look at this:' + data)
                );
        }

    }

我的服务的第二个文件如下所示:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class DataService {
    constructor(http:Http) {
        this.data = http.get('http;//...API')
            .map(response => response.json());
    }
}

...但控制台显示以下错误:

components/datavisualisation/dataservices/datavisualisation.output.service.ts:12:26 
Property 'data' does not exist on type 'DataService'.
components/datavisualisation/dataservices/datavisualisation.output.service.ts:14:29 
Property 'data' does not exist on type 'DatavisualisationOutput'.
components/datavisualisation/dataservices/datavisualisation.output.service.ts:16:43 Cannot find name 'data'.
components/datavisualisation/dataservices/datavisualisation.service.ts:8:13 
Property 'data' does not exist on type 'DataService'.

我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

您应该在data组件上定义DatavisualisationOutput属性:

export class DatavisualisationOutput {

    public data: any; //this one

    constructor(dataservice: DataService) {
        dataservice.data
            .subscribe(
                data => this.data = data,
                console.error,
                () => console.log('Look at this:' + data)
            );
    }

}

DataService

@Injectable()
export class DataService {

    public data: any;

    constructor(http:Http) {
        this.data = http.get('http;//...API')
            .map(response => response.json());
    }
}

DatavisualisationOutput ...只需..始终使用this定义您访问的任何媒体资源。

答案 1 :(得分:0)

正如@PierreDuc已经说过,你应该在组件类中定义该变量,使其在Class上下文中可用。

此外,您应该在服务中创建一个负责数据的方法。只需从另一个组件调用相同的方法,它将返回上次检索的数据。

代码

@Injectable()
export class DataService {
    data: any;
    constructor(http:Http) {
    }

    getData(){
       if(this.data) return Observable.of(this.data)
       else
          return http.get('http;//...API')
            .flatMap(response => {
               this.data = response.json();
               return Observable.of(this.data)
            );
    }
}

<强>组件

export class DatavisualisationOutput {
    myData: any;
    constructor(private dataservice: DataService) {
        dataservice.data
            .subscribe(
                data => this.data = data,
                console.error,
                () => console.log('Look at this:' + data)
            );
    }
    ngOnInit(){
        dataservice.getData().subscribe(
          data => myData = data
        )
    }
}