Best practice of Angular

Discussão em 'Angular' iniciado por DrK, Junho 11, 2025.

  1. DrK

    DrK Guest

    I've just finished my Angular lessons and I already find out some differences between what I learn and the Angular official documentation.

    Let's imagine I want to recover an user with ID of an API.

    Here is how I would do it according to my lessons :

    export class UserService {

    constructor(
    private httpClient: HttpClient
    ) {
    }

    public user: User; // local variable using User model
    public userSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);

    async getSingleUserFromServer() {
    await this.httpClient.get<any>('https://xvalor.repliqa.fr/api/v1/user/' + this.userId).subscribe(
    (response) => {
    this.user = response;
    this.userPortfolios = this.user.portfolioAssoc;
    this.emitSubjects();
    });
    }

    emitSubjects() {
    this.userSubject.next(this.user);
    }
    }


    and here is how angular doc procceed

    getHeroes (): Observable<Hero[]> {
    return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
    tap(_ => this.log('fetched heroes')),
    catchError(this.handleError<Hero[]>('getHeroes', []))
    );
    }


    I understand than both methods are quiet doing the same thing, I just want to be sure which one I should use, especially in big project developpement.

    Continue reading...

Compartilhe esta Página