Emit Observable value and handle it in component

Learner

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?

bryan60

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.

edited at
0

Comments

0 comments
Login to comment

Related

Emit new value to Observable

How to correctly create a component that can handle a value and an observable of this value

Combine subject and observable and wait for subject to emit a value then emit observable

Get the latest value of an Observable and emit it immeditely

combineLatest doesn't wait for observable to emit value

Rxjs: Execute code and emit value on completion of Observable

Emit value of the observable depends on others observables

How to emit only one or last value of Observable?

Emit data using Observable between Shared Service and Component

cant emit multiple value to another component Angular

catch value on event emit from child component in parent component

How should I emit a single value when observable completes?

How to get an observable's value without triggering new emit

RxSwift - How to retry when an observable changes but emit only last value?

fromEvent observable emit a value only on hit of enter key

How do I emit the value of an observable to an rxjs Subject?

RxJS: Share() an Observable & Emit Last Value to each new subscriber

RxJs Observable - Handle 404 by emitting default value

RxJS function that emit last value from one observable then other emit true

@Output emit value from directive to parent component Angular 4

TabIndex vue emit change tab value from another component

Emit value from vue-multiselect.js component to main view

Preserve observable value when component is reloaded

Render observable in component while waiting for next value?

Emit item on timeout in Observable

Conditionally emit observable

VueJs: How to pass a 'computed function' value from child component to the parent component using $emit(optionalPayload) and $on?

Is is possible to emit value on every observable completion that will go to the subscribers using single pipe?

How to get a shared observable to emit a new value from .startWith() every time a shared stream gets a new subscriber?