Currently I have this working
let cognitoUser: CognitoUser;
this.userService.cognitoUser.subscribe(
r => {
cognitoUser = r;
}
);
Is there a one liner like const cognitoUser = ....
Thank you
Currently I have this working
let cognitoUser: CognitoUser;
this.userService.cognitoUser.subscribe(
r => {
cognitoUser = r;
}
);
Is there a one liner like const cognitoUser = ....
Thank you
You can use async pipe.
In addition if You are in need of selecting only a single field from object returned by the observable, You can use a pluck operator.
You have a long list of operators to define a logic without having to subscribe for the observable, leaving that job to async pipeline. Another usefull pipe operator might be a map or any other filtering pipe.
Pluck/map will handle object transformation the observable emits and will return its value at the other end of the pipe, async will allow You to not subscribe in the code but rather leave the observable for the markup to subscribe whenever it is needed.
It won't make it as short as You probably want but it is better than nothing:
x.component.ts:
public observable$ = this.userService.cognitoUser;
x.component.html:
{{ observable$ | async }}
Your cognitoUser
is Observable
or BehaviourSubject
? BehaviourSubject
could return this.userService.cognitoUser.value()
which means last value of your BehaviourSubject
.