Incorporating Angular 13, my service contains the following observable:
private _user = new BehaviorSubject<ApplicationUser | null>(null);
user$ = this._user.asObservable();
The ApplicationUser model is defined as:
export interface ApplicationUser {
userName: string;
role: string;
}
I understand how to access user$ in the template with:
<li *ngIf="authService.user$ | async as user ; else login">
<a href="#" data-uk-icon="user">{{ user.userName }}</a>
</li>
However, I aim to manipulate it within my component. I have attempted:
user!: ApplicationUser;
ngOnInit(): void {
this.authService.user$
.subscribe((test: ApplicationUser) => {
this.user = test;
});
}
I have also tried:
this.user=this.authService.user$.subscribe();
This results in an error stating that Subscription is missing required properties (username, role).
Another attempt was:
this.authService.user$
.subscribe(test=> this.user = test);
However, a complaint arises indicating that Type 'ApplicationUser | null' cannot be assigned to type 'ApplicationUser'.
What should be the appropriate syntax in my component to store the observable data into my user variable?
Thank you