Observable.forkJoin()导致SyntaxError:意外的令牌

时间:2016-03-27 12:53:25

标签: angular

我构建了一个http包装器,它为所有请求添加了一些自定义标头。 现在我正在尝试实现某种预加载器,因此我希望观察当前处于活动状态的所有请求,并在完成所有操作后发出事件。

构造函数中的Observable.forkJoin导致错误:

angular2-polyfills.js:332 Error: SyntaxError: Unexpected token <(…)
ZoneDelegate.invoke @ angular2-polyfills.js:332
Zone.run @ angular2-polyfills.js:227
(anonymous function) @ angular2-polyfills.js:576
ZoneDelegate.invokeTask @ angular2-polyfills.js:365
Zone.runTask @ angular2-polyfills.js:263
drainMicroTaskQueue @ angular2-polyfills.js:482
ZoneTask.invoke @ angular2-polyfills.js:434

你们知道为什么吗?

这是我的服务:

import {Injectable, EventEmitter} from 'angular2/core';
import {Http, Response, RequestOptionsArgs, Headers} from 'angular2/http';
import {Observable} from 'rxjs/rx';

@Injectable()
export class RtcApiService {
    onLoadingStarted: EventEmitter<any> = new EventEmitter();
    onLoadingFinished: EventEmitter<any> = new EventEmitter();
    isLoading: boolean;
    private observables: Array<Observable<Response>> = new Array();    

    constructor(private _http: Http) {
        Observable.forkJoin(this.observables).subscribe((result) => {
            this.isLoading=false;
            this.onLoadingFinished.emit(null);
        });
    }

    get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        var observable = this._http.get(url, {
            headers: this.createHeaders()
        });
        this.watchObservable(observable);
        return observable;
    }

    post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>{
        var observable = this._http.post(url, JSON.stringify(body), {
            headers: this.createHeaders()
        });
        this.watchObservable(observable);
        return observable;
    }

    put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>{
        var observable = this._http.put(url, JSON.stringify(body), {
            headers: this.createHeaders()
        });
        this.watchObservable(observable);
        return observable;
    }

    private createHeaders(): Headers{
        var headers = new Headers();
        headers.append('Content-Type', 'application/json');
        headers.append('X-Requested-With', 'XMLHttpRequest');
        return headers;
    }

    private watchObservable(observable: Observable<Response>): void{
        this.observables.push(observable);
        if(!this.isLoading){
            this.isLoading = true;
            this.onLoadingStarted.emit(null);            
        }
    }
}

使用angular2 beta12

index.html看起来像这样:

...
<script src="lib/es6-shim.min.js"></script>
<script src="lib/system-polyfills.js"></script>
<script src="lib/shims_for_IE.js"></script>

<script src="lib/angular2-polyfills.js"></script>
<script src="lib/system.src.js"></script>
<script src="lib/Rx.js"></script>
<script src="lib/angular2.dev.js"></script>
<script src="lib/http.dev.js"></script>
<script src="lib/router.dev.js"></script>
....

0 个答案:

没有答案