与Observable.throw()打破

时间:2016-09-23 10:11:58

标签: angular rxjs5

我试图为http请求实现错误处理。我已经从angular doc https://angular.io/docs/ts/latest/guide/server-communication.html

复制了代码

这是我的档案:

import {Injectable} from "@angular/core";
import {Http, Response} from "@angular/http";
import '/js/admin/rxjs-operators';
import {Observable} from "rxjs";
@Injectable()
export class ApiService {
    private apiUrl: string = 'http://www.system.local/api/';

    constructor(private http: Http) {
    }

    post(url: string, params): Observable<any> {
        let requestUrl: string = this.apiUrl + url;
        return this.http.post(requestUrl, params)
            .map(response => response.json())
            .catch(this.handleError);
    }

    private handleError(error: any) {
        // In a real world app, we might use a remote logging infrastructure
        // We'd also dig deeper into the error to get a better message
        let errMsg = (error.message) ? error.message :
            error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }

    private parseParams(obj: Object, prefix?: string): string {
        var str = [];
        for (var p in obj) {
            if (obj.hasOwnProperty(p)) {
                var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
                str.push(typeof v == "object" ?
                    this.parseParams(v, k) :
                encodeURIComponent(k) + "=" + encodeURIComponent(v));
            }
        }
        return str.length ? "?" + str.join("&") : '';
    }
}

和rxjs-operators:

// import 'rxjs/Rx'; // adds ALL RxJS statics & operators to Observable
// See node_module/rxjs/Rxjs.js
// Import just the rxjs statics and operators we need for THIS app.
// Statics
import 'rxjs/add/observable/throw';
// Operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/toPromise';

当我刷新页面时,我得到即时错误:

zone.js:1274 GET http://www.system.local/node_modules/rxjs/ 404 (Not Found)
login:15 Error: (SystemJS) XHR error (404 Not Found) loading http://www.system.local/node_modules/rxjs(…)

当我评论// return Observable.throw(errMsg);时,一切正常,但Observable不会进一步传递。 它能是什么? 我正在使用rxjs@5.0.0-beta.12

0 个答案:

没有答案