Angular 2 Observable.forkJoin this._subscribe is n

2019-07-18 12:57发布

问题:

I have this Angular2 Component, where I try to make two HTTP parallel requests to with PersonServie (that works fine). But the Observable.forkJoin method throws this error: EXCEPTION: TypeError: this._subscribe is not a function in [null]

Evaluating getIdentificationTypes() and getPerson() function, each return an Observable object.

What am I missing?

 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);
        }
}

回答1:

Try to use Observable.of(person) instead of Observable.create which is used incorrectly.



回答2:

I think get it.

The problem was because i was importing import { Observable } from 'rxjs/Rx'; instead of import { Observable } from 'rxjs/Observable';

What is the diference?