Angular-如何用不同的参数(多个API调用)预订相同的服务响应?

时间:2019-07-12 22:47:48

标签: javascript angular typescript rest web-services

下面是我的代码段

角度服务

npm install concurrently --save

"scripts": {
    "start": "concurrently \"npm run server\" \"react-scripts start\"",
    "server": "nodemon server/app",
    "build": "react-scripts build",
...

角度组件

export class CarService {
  constructor(private httpClient: HttpClient) {}

  // -------------------------------------------------
  // define observable Subject(s) which will be subscribed by callers
  private observableList = new Subject<CarModel>();
  public getObservableList() {
    return this.observableList.asObservable();
  }
  // -------------------------------------------------

  // fetch required required data, then add into observable 
  public fetchCarInfo(carId: string) {
    // call service
    const url = '.../api/car/' + carId;
    this.httpClient
      .get<CarModel[]>(url)
      .subscribe(
        data => {
          this.observableList.next(data);
        }
      );
  }
}

正常解决方案
要添加到 Service 2个可观察对象 Component 两者中,请同时订阅
但是我认为这不是一个干净的解决方案,即在服务中添加多个Observable等于与具有不同参数的服务调用数量相等。

有什么建议吗?

0 个答案:

没有答案