简体   繁体   中英

angular2 rxjs .map does't work in version 2.0.1

This code works on angular2 v.2.0.0 .rc.2 but does't on 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";

I can import this for use .map right?

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;

I have a list of food in my controller, 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")

there's .map does't work, can't we use .map in angular2 2.0.1?

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

Do I need to create Services? How to fix this, thank you

Try updating to Typescript 2.0.3

This solved the issue for me.

And make sure you are using ECMAScript 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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