如何同步两个可观察物?

时间:2019-05-06 18:37:36

标签: angular typescript rxjs

我在一个类中有一个类型为any的可观察对象,我需要在一个组件中使用它。

我的课堂上有一个可观察的对象,还有一个使用http请求更新该可观察对象的方法。

export class BusquedaService {
  resultados$: Observable<any>;
  constructor(private http: HttpClient) { }
  buscar( termino: string ) {
    this.resultados$ = this.http.get(`${environment.urlAPI}/products?&name=${termino}`).pipe(
      map( (data : any) => {
        return data.products;
      })
    );
  }
}

我有一个组件,需要在其中使用此可观察的值并读取其值。

export class BusquedaComponent implements OnInit {
  resultados$: Observable<any>;
  constructor(private busquedaService: BusquedaService) { }
  ngOnInit() {
    this.resultados$ = this.busquedaService.resultados$;
  }
}

因此,在此组件中,我声明了一个类型为no的可观察对象,并在ngOnInit()中将其在服务中的值赋给了它。问题在于此值(组件内部的变量)不会更新。

我可以在组件中将该服务声明为公共服务,以便在HTML上使用它,但是我想知道是否有一种方法可以像我尝试的那样进行操作。

我该怎么做?

0 个答案:

没有答案
相关问题