本文介绍了Angular 2 Observable.fork加入this._subscribe不是[null]中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Angular2组件,在这里我尝试使用PersonServie发出两个HTTP并行请求(效果很好).但是Observable.forkJoin方法抛出此错误:EXCEPTION: TypeError: this._subscribe is not a function in [null]

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]

计算getIdentificationTypes()getPerson()函数,每个函数返回一个Observable对象.

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

我想念什么?

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

推荐答案

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

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

这篇关于Angular 2 Observable.fork加入this._subscribe不是[null]中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:41