【问题标题】:Angular 2 build objects from http get requestAngular 2从http get请求构建对象
【发布时间】:2016-10-11 14:23:54
【问题描述】:

我已经开始学习 Angular 2。我尝试通过 http get 请求获取一些数据,然后我想用这些数据构建对象,以便以后可以用模板显示它们。如果我想错了,你可以告诉我。

我有我的模型 AnalyticsData:

export class AnalyticsData {
     pagePath: string;
     pageViews: number;
     uniquePageViews: number;
     avgTimeOnPage: number;
     entrances: number;
     bounceRate: number;

    constructor(object?: any) {
        this.pagePath = object && object.pagePath || null;
        this.pageViews = object && object.pageViews || null;
        this.uniquePageViews = object && object.uniquePageViews || null;
        this.avgTimeOnPage = object && object.avgTimeOnPage || null;
        this.entrances = object && object.entrances || null;
        this.bounceRate = object && object.bounceRate || null;
    }

}

我的数据服务:

export class DataService {

    private dataUrl: string = 'http://example.com/app/analyticsdata';
    constructor(private http: Http) { }

    getData() {
        return this.http.get(this.dataUrl)
            .map((response: Response) => response.json());
    }

}

我的分析组件:

export class AnalyticsComponent implements OnInit {

    myData: Array<AnalyticsData>;

    constructor(private services: DataService) { }

    ngOnInit(): void {
        this.getData();
    }

    getData() {
        this.services.getData()
            .subscribe(
            function (response) {
                response.forEach((element: AnalyticsData, index: number) => {
                    this.myData.push(
                        new AnalyticsData({
                            pagePath: element['ga:pagePath'],
                            pageViews: element.pageViews,
                            uniquePageViews: element.uniquePageViews,
                            avgTimeOnPage: element.avgTimeOnPage,
                            entrances: element.entrances,
                            bounceRate: element.bounceRate
                        })
                    );
                });
            },
            function (error) { console.log("Error happened" + error) },
            function () {
                console.log("the subscription is completed");
            }
            );

    }

}

上面的错误是:EXCEPTION: Cannot read property 'push' of undefined。我不明白为什么会这样,因为我在类的顶部分配了变量 myData

【问题讨论】:

  • myData: Array&lt;AnalyticsData[]&gt;;
  • 将此添加到您的构造函数并重试:this.MyData = [];
  • @HarryNinh 谢谢我将您的回答与 micronyks 的回答结合起来解决我的问题。

标签: javascript angular typescript


【解决方案1】:

同样使用arrowFunction()=&gt;如下图,

getData() {
        this.services.getData()
            .subscribe(
            (response) => {                                       //<<<<===here
                response.forEach((element: AnalyticsData, index: number) => {
                    this.myData.push(
                        new AnalyticsData({
                            pagePath: element['ga:pagePath'],
                            pageViews: element.pageViews,
                            uniquePageViews: element.uniquePageViews,
                            avgTimeOnPage: element.avgTimeOnPage,
                            entrances: element.entrances,
                            bounceRate: element.bounceRate
                        })
                    );
                });
            },
            (error) => { console.log("Error happened" + error) }, //<<<===here
            () => {                                               //<<<===here
                console.log("the subscription is completed");
            }
            );

    }

【讨论】:

  • 谢谢。我还需要将this.myData = []; 放在承包商中,就像@HarryNinh 说的那样
猜你喜欢
  • 2018-11-27
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多