I have a login()
method an AuthService
. What I want is to emit value from login()
method in AuthService
to handle value in NavbarComponent
:
export class AuthService extends AbstractRestService<any> {
user: AppUser = {id: null};
login(userName: string, password: string) {
return this.http
.post('/auth/login')
.pipe(
map(res => {
// here I want to emit value of Observable `anUser$`
// to send value to NavbarComponent
this.getAppUser(); // I want to throw Observable value to handle its value
// in NavbarComponent.ts
return true;
})
, catchError(this.handleError)
);
}
public getAppUser(): Observable<AppUser> {
return of(this.user);
}
}
My NavbarComponent.html
looks like this:
<li *ngIf="appUser$ | async as anUser">
{{ anUser?.name }}
</li>
My NavbarComponent.ts
looks like this:
appUser$: Observable<AppUser>;
async ngOnInit() {
this.appUser$ = this.auth.getAppUser();
}
What I want is to emit value from login()
method in AuthService
to handle value in NavbarComponent
, however value of appUser$
is not shown in NavbarComponent.html
.
Could you tell me what I am doing wrong?
do like this:
export class AuthService extends AbstractRestService<any> {
user: AppUser = {id: null};
login(userName: string, password: string) {
return this.http
.post('/auth/login')
.pipe(
map(res => {
this.userSource.next(res); // just call next with the user here
return true;
})
, catchError(this.handleError)
);
}
private userSource = new BehaviorSubject<AppUser>({id: null})
public getAppUser = this.userSource.asObservbale();
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments