Angular 2 Observable.forkJoin this._subscribe不是[null]中的函数

时间:2016-04-18 23:51:59

标签: angular observable

我有这个Angular2组件,我尝试使用PersonServie发出两个HTTP并行请求(工作正常)。但是Observable.forkJoin方法会抛出此错误:EXCEPTION: TypeError: this._subscribe is not a function in [null]

评估getIdentificationTypes()getPerson()函数,每个函数返回一个Observable对象。

我错过了什么?

 import { Component, OnInit } from 'angular2/core';
    import { Router, RouteParams } from 'angular2/router';
    import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router';
    import { Observable } from 'rxjs/Rx';
    import 'rxjs/Rx';

    // more imports for models and other components//


    @Component({
        selector: 'my-coponent',
        templateUrl: 'template',
        directives: [ROUTER_DIRECTIVES]
    })

    export class MyComponent implements OnInit {
           public identificationTypes: IdentificationType[];
           public person : Person;
       // some public and private properties here //

        constructor(
            private _router: Router,
            private  _routeParams : RouteParams,
            private _personService: PersonServie
        ){}

        ngOnInit() {
            let id= (this._routeParams.get('id'));
            this._id= id? +id: 0;

            Observable.forkJoin([
               this.getIdentificationTypes(),
               this.getPerson(this._id)
            ]).subscribe(data =>{
                this.identificationTypes= data[0];
                this.person= data[1];              
            });
        }

        private getIdentificationTypes(){
            return this._generalService.getIdentifiacitonTypes();
        }

        private getPerson(person: number){
            if(athleteId == 0){                
                let person = new Person();
                return Observable.create(person );
            }


            return this._personService.getPerson(athleteId);
        }
}

2 个答案:

答案 0 :(得分:9)

尝试使用Observable.of(person)代替错误使用的Observable.create

答案 1 :(得分:2)

我认为得到它。

问题是因为我导入import { Observable } from 'rxjs/Rx'; 而不是import { Observable } from 'rxjs/Observable';

有什么不同?