Angular 2 Observable.forkJoin this._subscribe is n

2019-07-18 12:29发布

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

2条回答
ら.Afraid
2楼-- · 2019-07-18 13:01

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?

查看更多
淡お忘
3楼-- · 2019-07-18 13:05

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

查看更多
登录 后发表回答