Angular 2:错误:src / app / mage / mage.component.ts(19,98):','预期。)app / mage / mage.component.ts(19,100):找不到名称'response'。)

时间:2017-02-02 10:18:49

标签: angular service

我在尝试将角度2服务挂钩到另一个Angular 2组件时出现此错误:

ERROR in ./src/app/mage/mage.component.ts
Module build failed: Error: /Applications/MAMP/htdocs/angular2/ng2/src/app/mage/mage.component.ts (19,98): ',' expected.)
/Applications/MAMP/htdocs/angular2/ng2/src/app/mage/mage.component.ts (19,100): Cannot find name 'response'.)

为什么会这样?如果可能的话,尽量帮助我,不要贬低,因为这是我第二天使用Angular 2,我非常喜欢Angular 2.我很好地问这个因为我的最后一个Angular 2问题被downvoting杀死了,进入负分。谢谢你没有消极地对我说地狱的深处:)

这是mage.component.ts:

import { Component } from '@angular/core';
import { MageService } from './mage.service';
import { OrdersStats } from './orders.stats';
import { OrderStatusStat} from './order.status.stat';

@Component({
  selector: 'my-mage',
  templateUrl: './mage.component.html',
  styleUrls:['./mage.component.css']
})
export class MageComponent {

  ordersStatistics:OrdersStats; 

  constructor(private mageService: MageService) { }

  ngOnInit() {
      this.mageService.getMagentoData().subscribe(response => <OrdersStats>this.ordersStatistics = response);
  }
}

这是orders.stats.ts:

export class OrdersStats {

    total:number;
    orderStatusesStats:OrderStatusStat[];
}

这是mage.service.ts:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Response } from '@angular/http';
import { OrderStatusStat } from './order.status.stat';
import { OrdersStats } from './orders.stats';
import 'rxjs/add/operator/map';

@Injectable()
export class MageService {

  constructor(private http: Http) { }

  getMagentoData() {
      return this.http.get("http://localhost:80/angular2/ng2/middleware/MiddleWare.php")
      .map(response => <OrdersStats>response.json().data);
  }

}

这是orders.status.stat.ts:

export class OrderStatusStat {

    status:string; //Name of the status
    count:number; //How many orders with this status
    totalAmount:number; //Total amount of all orders with this status

}

1 个答案:

答案 0 :(得分:3)

你不能在箭头函数的短手风格中使用强制转换,你应该这样写:

ngOnInit() {
   this.mageService.getMagentoData().subscribe((response) => {
        this.ordersStatistics = <OrdersStats>response;
   });      
}