angular2 rxjs .map在版本2.0.1中不起作用

时间:2016-10-10 04:48:41

标签: json asp.net-mvc angular

此代码适用于angular2 v.2.0.0 .rc.2但不适用于angular2 v2.0.1

app.appcomponent.ts

import { Component, OnInit } from "@angular/core";
import { Injectable } from '@angular/core';
import { Http, Response, URLSearchParams } from "@angular/http";    
import { IFood } from "./food";
import { Headers, RequestOptions } from "@angular/http";

我可以将其导入使用.map对吗?

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/catch';

@Component({
selector: "my-app",
template: `
    <h1>My First Angular 2 App</h1>
        <ul *ngFor='let food of foods'>
            <li>{{food.foodName}}</li>
        </ul>
    `,
templateUrl: "./app/form.html"
})

@Injectable()
export class AppComponent implements OnInit {
public values: string[];
public headers: Headers;
errorMessage: string;

我的控制器里有食物清单,foodid,foodname

foods: IFood[];
foodModel = <IFood>{}; 

constructor(private http: Http) {
    this.http = http;
    this.headers = new Headers();
    this.headers.append("Content-Type", "application/json");

  }

 ngOnInit() {
    return this.http.get("/home/getdata")

.map不起作用,我们不能在angular2 2.0.1中使用.map吗?

        .map((response: Response) => <IFood[]>response.json())
        .subscribe(data => this.foods = data, error => 
            this.errorMessage =  <any>error);
}     
}

我是否需要创建服务? 如何解决这个问题,谢谢

1 个答案:

答案 0 :(得分:0)

尝试更新到Typescript 2.0.3

这解决了我的问题。

确保使用ECMAScript 6