Angular 2 Observable.forkJoin this._subscribe is not a function in [null]

谁说我不能喝 提交于 2019-12-23 22:35:20

问题


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?



来源:https://stackoverflow.com/questions/36706198/angular-2-observable-forkjoin-this-subscribe-is-not-a-function-in-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!