RxJs get value from observable

前端 未结 3 1691
盖世英雄少女心
盖世英雄少女心 2020-12-06 03:58

In component :

singleEvent$: Observable;

On init, I get observable

this.singleEvent$ = this._eventService.even         


        
3条回答
  •  执念已碎
    2020-12-06 04:51

    You can use observable.pipe(take(1)).subscribe to limit the observable to get only one value and stop listening for more.

    let firstName: string; 
    
            this.insureFirstName
                .pipe(take(1))  //this will limit the observable to only one value
                .subscribe((firstName: string) => {
                    this.firstName = firstName; asssgning value 
                });
            console.log(this.firstName); //accessing the value
    

提交回复
热议问题