💥Angular Mistakes #1: ✋ DON'T Overuse the Async Pipe #angular

preview_player
Показать описание


This video will talk about a very common Angular mistake, the overuse of the async pipe to access data observables in multiple places in the template.

We will present both the mistake and the solution.

Timestamps:

0:00 Intro
01:03 - Angular Mistake 1: The Problem
03:20 - Angular Mistake 1: The Solution

Related articles on the Angular University blog:

Free content at the Angular University:

Follow me:

Рекомендации по теме
Комментарии
Автор

Tip: If you're using the latest version of RxJS, you can "simplify" the data$ observable further.
1. Define data$ directly in its declaration. No need for ngOnInit.
2. Use type inference. No need for manually typing Observable<PageData>, and no need for an extra interface.
3. Use an object directly in combineLatest. No need for an array + mapping to an object.

data$ = combineLatest({ courses: this.courses$, lessons: this.lessons$, users: this.users$ });

nunoarruda
Автор

Thanks a lot. Your videos and blog posts combine simplicity and depth in a perfect way. Thanks again.

esayed
Автор

Super, thanks for sharing. Looking forward for new one.

aleksandrm
Автор

Thank you so much, sir! Your content is extremely valuable! Please keep up the good work.

andriesvanwyk
Автор

Thank you for the video. I personally prefer using toSignal for each observable, then simply use computed to combine the three signal into a single source which could be easily consumed in the template.

abees
Автор

We can something like this as well
<ng-container *ngIf="{ courses: (courses$ | async), activePage: (activePage$ | async), isLoading: (isLoading$ | async) } as vm">

icoz
Автор

Many thanks for sharing! I really appreciate your work!
Just one question, what if you need to display a loading for each block of data (courses, lessons, users)? Would you still have a single observable and async pipe and you would emit each time the data is available?
If so, would not it be a "performance issue"? I mean, if I update courses loading, it will emit this change to the single data observable and it will refresh the whole template, isn't it?

MrDrogoyonk
Автор

What about writing

@if ({
courses: courses$ | async,
lessons: lessons$ | async,
users: users$ | async
} as params) {
...
}

at the top of the component template? I think it is easier than using combineLatest in the component, isn't it?
4:31

antonisakov
Автор

I would just use an ngTemplateOutlet. I also think that an if statement with an object will always be truthy, even if one of the properties is undefined or null, because combineLatest will emit when any of the included observables emit, so you might get an issue here if you remove completely some of the ifs

theGoldyMan
Автор

If a given component requires data from multiple sources, I always use a single resolver with a fork join. In my opinion, placing (complex) data handling in a component with conditional rendering is poor design. The whole idea behind resolvers is to avoid these extra calculations so that the component can be assured that the data is already available when the component is initialized.

KiffinGish
Автор

can we use computedAsync with signas and solve the same problem?

ratg
Автор

How would you manage errors with this pattern?

Mattakattak
Автор

combineLatest will not emit an initial value until each observable emits at least one value.

LarsRyeJeppesen
Автор

PageData should be type (alias), not interface

nomoredarts