angularJS,做post-map-catch,但找不到catch函数

时间:2017-11-20 21:23:21

标签: angular rxjs

我已经像这样导入了所有需要的模块:

import {Message} from "./message.model";
import {Http, Response} from "@angular/http";
import {Injectable} from "@angular/core";
import 'rxjs/Rx';
import {Observable} from "rxjs/Rx";

可以找到map函数,但不能找到catch函数。 任何人有任何想法,请通知我,谢谢你。 下面附带的代码也可以在截图中找到。 screenshot of the code

@Injectable
export class MessageService {

    private messages: Message[] = [];

    constructor(private http: Http) {}

    addMessage (message: Message) {
        this.messages.push(message);
        const body = JSON.stringify(message);
        const headers = new Headers({'Content-Type': 'application/json'});
        // return this.http.post('http://localhost:3000/message', body)
        //     .map((response: Response) => response.json())
        //     .catch((err: Response) => Observable.throw(error.json()));
        return this.http.post('http://localhost:3000/message', body, {headers: headers})
            .map((response: Response) => {
                const result = response.json();
                const message = new Message(result.obj.content, 'Dummy', result.obj._id, null);
                this.messages.push(message);
                return message;
            })
            .catch((error: Response) => Observable.throw(error.json()))
    }

    getMessage () {
        return this.messages;
    }

    deleteMessage (message: Message) {
        this.messages.splice(this.messages.indexOf(message), 1);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试添加运算符,如下所示:

import 'rxjs/add/operator/catch';