A BehaviorSubject
is a type of RxJS Subject
that allows initialization with an initial value and emits its current value (which can be fetched by getValue
method) to new subscribers.
Here's an example of how to use the BehaviorSubject
function in Typescript:
index.ts393 chars11 lines
In the example above, we first import the BehaviorSubject
function from the rxjs
library. We then create a new instance of the BehaviorSubject
with an initial value of "Initial value"
. Next, we subscribe to the BehaviorSubject
and log its current value (which is "Initial value"
) using the subscribe
method. Finally, we update the value of the BehaviorSubject
using the next
method with the argument "New value"
, which will trigger the BehaviorSubject
to emit this new value and all its subscribers will receive the updated value.
gistlibby LogSnag