简体   繁体   English

来自http的Angular 2构建对象获取请求

[英]Angular 2 build objects from http get request

I've start learning angular 2. I try to get some data via http get request and then I would like to build objects with that data so I can display them with template later. 我已经开始学习angular2。我尝试通过http get请求获取一些数据,然后我想用该数据构建对象,以便以后可以在模板中显示它们。 If I am thinking with the wrong way you can tell me. 如果我以错误的方式思考,您可以告诉我。

I have my model AnalyticsData: 我有我的模型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;
    }

}

My DataService: 我的数据服务:

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());
    }

}

My AnalyticsComponent: 我的Analytics(分析)组件:

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");
            }
            );

    }

}

The error with the above is: EXCEPTION: Cannot read property 'push' of undefined . 上面的错误是: EXCEPTION: Cannot read property 'push' of undefined I don't understand why this happened because I've assign the variable myData on the top of the class. 我不明白为什么会这样,因为我已经在类的顶部分配了变量myData

Also use arrowFunction ()=> as shown below, 还要使用arrowFunction ()=> ,如下所示,

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");
            }
            );

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM