简体   繁体   English

Angular-如何从另一个可观察对象返回一个可观察对象

[英]Angular - How to return an observable from another observable

I have a component subscribing to an observable in a service. 我有一个订阅服务的组件。 That method in turn is subscribing to an observable in a different service. 该方法继而订阅了其他服务中的可观察对象。 I want to pass an array from the last service back to the first service, which in turn passes that array back to the component. 我想将数组从最后一个服务传递回第一个服务,后者又将该数组传递回组件。 More specifically, the component calls its local service, which then calls a data service that hits my database with an http client. 更具体地说,该组件调用其本地服务,然后调用本地服务,该数据服务使用http客户端访问我的数据库。 The http client is working and the data service returns the array to the local service. http客户端正在运行,数据服务将数组返回到本地服务。 The local service receives the array, but I can't figure out how to then pass that array back to the component as an observable. 本地服务接收到该数组,但是我无法弄清楚如何将该数组作为可观察的内容传递回组件。 Here are the short code blocks: 以下是简短的代码块:

Component: 零件:

this.soccerService.getPlayers(0).subscribe(
  teamPlayers => {
    this.teamPlayers = teamPlayers;
    this.currentTeam = teamPlayers.team;
    this.players = teamPlayers.players;
    this.teamColor = this.currentTeam.color;
  }

Soccer Service 足球服务

this.dataService.getPlayers(teamId).subscribe( players => { 
            this.players = players;
            this.teamPlayers.team = this.team;
            this.teamPlayers.players = this.players;

            this.teamPlayers = {
                team: this.team,
                players: players
            };
            return of(this.teamPlayers);
        });  

Data Service 数据服务

getPlayers(id): Observable<Player[]> {
debugger;
return this.http.get<Player[]>(apiRootCustom + '/GetPlayers/' + id, httpOptions);

} }

You're using subscribe in your soccer service. 您正在足球服务中使用subscribe What you want to do is pass back the observable from your data service, and have your soccer service augment the response a bit before continuing to pass it back to your component. 您想要做的是从数据服务中传回可观察对象,并让您的足球服务在继续将其传递回组件之前稍微扩大响应。

Think of subscribe as the "end of the road" for your observable, but you can pass the observable around to any number of subscribers and perform different operations on the response at anytime using a pipe. subscribe视为可观察对象的“道路的尽头” ,但是您可以将可观察对象传递给任意数量的订阅者,并随时使用管道对响应执行不同的操作。

Example of using different operators to change the response of an observable for different subscribers: StackBlitz 使用不同的运算符更改不同订户的可观察对象的响应的示例: StackBlitz

In your code try something like this: 在您的代码中尝试如下操作:

Compoent Compoent

this.soccerService
  .getPlayers(0)
  .subscribe(
    (teamPlayers) => {
      this.teamPlayers = teamPlayers;
      this.currentTeam = teamPlayers.team;
      this.players = teamPlayers.players;
      this.teamColor = this.currentTeam.color;
    },
    (error: any) => {
      // TODO: handle any errors
    }
  );

Soccer Service 足球服务

this.dataService
  .getPlayers(teamId)
  .pipe(
    map((players) => {
      this.players = players;
      this.teamPlayers.team = this.team;
      this.teamPlayers.players = this.players;

      this.teamPlayers = {
        team: this.team,
        players: players
      };

      return this.teamPlayers;
    })
  );

Data Service 数据服务

  getPlayers(id): Observable<Player[]> {
    return this.http.get<Player[]>(`apiRootCustom/GetPlayers/${id}`, httpOptions);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM