In my angular app I'm using async pipe to render acomponent multiple times
<app-main-chart [type]="type" [name]="Name" [values]="values$ | async" [objectifs]="dataObjectifs"></app-main-chart>
...
...
<app-main-chart [type]="type" [name]="Name" [values]="values$ | async" [objectifs]="dataObjectifs"></app-main-chart>
The problem is that is causing multiple http request.
how can I use only one request ?
You can use shareReplay
operator.
Share source and replay specified number of emissions on subscription.
import { shareReplay } from 'rxjs/operators';
values$ = source
.pipe(
shareReplay()
);
You can subscribe in the controller and use that value in the template.
ngOnInit() {
this.values$.subscribe(values => this.values = values);
}
Then in the template:
<app-main-chart [type]="type"
[name]="Name"
[values]="values"
[objectifs]="dataObjectifs">
</app-main-chart>
You can wrap it in ngIf
block:
<div *ngIf="values$ | async as values">
<app-main-chart [type]="type"
[name]="Name"
[values]="values"
[objectifs]="dataObjectifs">
</app-main-chart>
<app-main-chart [type]="type"
[name]="Name"
[values]="values"
[objectifs]="dataObjectifs">
</app-main-chart>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With