RxJS multiple subscriptions in Angular 5

Viqas

Apologies if this is a duplicate questions, but I am new to RxJS and Angular, and I wasn't quite sure what to search.

I have the following Angular component which has 2 nested components:

<div>
    <h1>Parent Container</h1>

    <nest-one></nest-one>
    <nest-two></nest-two>
</div>

Each of those nested components can call the method on my service, which executes a http call:

getProduct(id: number): Observable<Product[]>  {
    return this.http.get('product/' + id)
    .map((response: Response) => {
      return response;
    })
    .catch(this.handleError);
  }

And both nested components subscribe to this method call like so:

this.productService.getProduct(id)
  .subscribe(
    data => {
      this.product = data;
    }
  );

My questions is, how can I, or is it possible to call the method getProduct in my productService and for both nested components' subscription to be executed, regardless of which component I call it from?

This is a simplified example of what I'm trying to do. Another option could be to have a base class and use that instead, but I want to see if this is possible or not.

Bene

You should use your parent container to handle this. Add an output to both components for when you want to load a new product an one input to pass the retrieved product down to them.

parent.ts

loadProduct$ = new Subject<string>();
product$ = this.loadProduct$.switchMap(id => this.productService.getProduct(id)).share();

parent.html

<div>
      <h1>Parent Container</h1>

      <nest-one [product]="product$ | async"  (loadProduct)="loadProduct$.next($event)></nest-one>
      <nest-two [product]="product$ | async"  (loadProduct)="loadProduct$.next($event) ></nest-two>

  </div>

Now both children are getting the new product regardless of which initiated the call. The share operator makes sure that there are no multiple streams and the backend is only called once. This is one way to do it - if you describe your use case more detailed, we may give you better answers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Working with multiple nested subscriptions in sequence (Angular, RxJs)

RXJS: Multiple Subscriptions in Order

Multiple subjects and subscriptions or only one - Angular 5

RxJs 6 chaining multiple subscriptions

Angular rxjs multiple subscriptions to ajax response observable firing multiple ajax requests

RxJS Multiple subscriptions for dynamically allocated buttons

Rxjs nested subscribe with multiple inner subscriptions

Observables (rxjs) nested subscriptions in angular project

Angular handle multiple dependent subscriptions

Angular 5 Rxjs Subject.subscribe() not triggering in multiple components

How to chain multiple subscriptions to an observable, within the observable, in rxjs?

How can I avoid multiple nested subscriptions using RxJS operators?

Angular & RxJS Duplicate Code with subscriptions - moving the code to a service, types and unsubscribe

Angular 2 - Handling multiple subscriptions on a single observable

Angular 5 - Rxjs forkJoin

ngrx, rxjs, and angular 5

Simplifying nested subscriptions in RxJS

Refactoring chained RxJs subscriptions

Chaining subscriptions in RxJS

RxJs Observables nested subscriptions?

Unnesting subscriptions with RXJS

Tree Shaking RxJS Angular 5

angular - Error handling with nested subscriptions - Chain multiple api-calls

Angular 5 Webpack 4 Rxjs 5 DllPlugin - Duplicated RxJs everywhere

How can I chain multiple dependent subscriptions using rxjs operators and transformations?

How to monitor number of RXJS subscriptions?

What happens to previous Subscriptions in RxJS?

RxJs unsubscribe unsibscribes future subscriptions

RxJs Observables nested subscriptions with takeUntil