Get current value from Observable without subscribing (just want value one time)

前端 未结 5 421
忘了有多久
忘了有多久 2020-12-13 17:15

The title says it all really. How can I get the current value from an Observable without subscribing to it? I just want the current value one time and not new values as they

5条回答
  •  情话喂你
    2020-12-13 17:30

    Not sure if this is what you are looking for. Probably write that behaviorsubject in a service. Declare it as a private and expose only the value that you have set. Something like this

     @Injectable({
       providedIn: 'root'
     })
      export class ConfigService {
        constructor(private bname:BehaviorSubject){
           this.bname = new BehaviorSubject("currentvalue");
        }
    
        getAsObservable(){
           this.bname.asObservable();
        }
     }
    

    This way the external users only have access to subscribe to the behaviorSubject and you get to set the desired value in the service.

提交回复
热议问题