Задача

Отрефакторить компонент: убрать ручные подписки, утечку памяти и лишний код. Объяснить преимущества async pipe.

@Component({
    selector: 'user-list',
    template: `
        <div *ngFor="let user of users">{{ user.name }}</div>
        <p>Total: {{ total }}</p>
    `,
})
export class UserListComponent implements OnInit {
    users: User[] = [];
    total = 0;
 
    constructor(private userService: UserService) {}
 
    ngOnInit(): void {
        this.userService.getUsers().subscribe((users) => {
            this.users = users;
            this.total = users.length;
        });
        // нет отписки — утечка
    }
}

Проблемы

Решение

См. также