How to avoid that when enrolling in an observable it receives the last amount issued?

Asked

Viewed 62 times

0

I have a behaviorSubject that emits values and an Observable I use to receive the values issued:

observableSource = new BehaviorSubject(null);
observable$: Observable<string> = this.observableSource.asObservable();

At the start of my component he signs up for this Observable through a Subscription:

ngOnInit() {
   this.mySubscription = this.myService.observable$.subscribe((res) => {
      console.log(res)
   })
}

ngOnDestroy() {
   this.mySubscription.unsubscribe();
}

Currently when my component is destroyed and started again, it receives a new inscription from my Observable, it has as initial value the last value issued.

Is there any way to sign me up for a Observable, it does not issue the last value but the initial value null?

1 answer

3


By default Behaviorsubject sane hot that is has this behavior of issuing the value when subscribed, so they need a value to boot.

In your case best use only Subject even though by default they are Cold.

observableSource = new Subject();
observable$: Observable<string> = this.observableSource.asObservable();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.