Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Angular 2中具有两个http.get调用的可观察类型_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 在Angular 2中具有两个http.get调用的可观察类型

Javascript 在Angular 2中具有两个http.get调用的可观察类型,javascript,angular,typescript,Javascript,Angular,Typescript,在我的ng2服务中,我有一个具有2个http.get调用的方法。 该函数如下所示: getInfo(userId: number): any { this.http .get(apiUrl, options) .map(response => response.json()) .subscribe(example => { this.example.FirstName = example.FirstName;

在我的ng2服务中,我有一个具有2个http.get调用的方法。 该函数如下所示:

getInfo(userId: number): any {

    this.http
       .get(apiUrl, options)
       .map(response => response.json())
       .subscribe(example => {
           this.example.FirstName = example.FirstName;
           this.example.LastName = example.LastName;

           this.http
               .get(`/api/${userId}`)
               .map(response => response.json())
               .subscribe(example => {
                   this.example.Street = example.Street;
                   this.example.City = example.City;

                   return this.example;
               });
       });
 }
唯一的问题是,在我的组件中,我不能订阅这个函数,因为它不是可观察的类型

如果我将函数的类型any替换为Observable,我得到:

声明类型既不是“void”也不是“any”的函数必须返回值

但在响应之后,我确实返回了一个值

如果没有两个单独的函数,我如何做到这一点


是的,我确实检查了这个答案:

试着把这个作为你的方法体写下来,这是一种方法,还有其他方法可以解决这个问题

return this.http
   .get(apiUrl, options)
   .map(response => response.json())
   .flatMap(example => {
       this.example.FirstName = example.FirstName;
       this.example.LastName = example.LastName;

       return this.http
           .get(`/api/${userId}`)
           .map(response =>  {
               let example =response.json();
               this.example.Street = example.Street;
               this.example.City = example.City;

               return this.example;
           });
   });

试着把它写成你的方法体,这是一种方法,还有其他方法可以解决这个问题

return this.http
   .get(apiUrl, options)
   .map(response => response.json())
   .flatMap(example => {
       this.example.FirstName = example.FirstName;
       this.example.LastName = example.LastName;

       return this.http
           .get(`/api/${userId}`)
           .map(response =>  {
               let example =response.json();
               this.example.Street = example.Street;
               this.example.City = example.City;

               return this.example;
           });
   });

使用rxjs/Rx flatMap的我的解决方案:

import {Observable} from 'rxjs/Rx';


ngOnInit() {
  const usersId = ['USR001', 'USR003'];
  this.doRequest(usersId);
}

doRequest(queryArr, previousObservable = null) {
        if (queryArr.length) {
            const url = 'api/' + queryArr.shift();
            let observable = null;
            if (previousObservable) {
                observable = previousObservable.flatMap(() => {
                    return this.http.get(url);
                });
            } else {
                observable = this.http.get(url);
            }
            return this.doRequest(queryArr, observable);
        } else {
            return previousObservable;
        }
    }

使用rxjs/Rx flatMap的我的解决方案:

import {Observable} from 'rxjs/Rx';


ngOnInit() {
  const usersId = ['USR001', 'USR003'];
  this.doRequest(usersId);
}

doRequest(queryArr, previousObservable = null) {
        if (queryArr.length) {
            const url = 'api/' + queryArr.shift();
            let observable = null;
            if (previousObservable) {
                observable = previousObservable.flatMap(() => {
                    return this.http.get(url);
                });
            } else {
                observable = this.http.get(url);
            }
            return this.doRequest(queryArr, observable);
        } else {
            return previousObservable;
        }
    }
值得一看值得一看