BehaviorSubject in Angular

How and where to use rxjs BehaviorSubject is our application?

BehaviorSubject is a part of RXJS library not specific to Angular part we can use this library with React, Vue etc. The question is why we need to use RXJS in Angular for example we want to pass data in any component in our app tree we can not use input and output decorator because input and output decorator only works in parent child relation but this is not the case here we want to push data in anywhere in our application the solutions BehaviorSubject with BehaviorSubject we can pass data to any component in our application tree. In the demo when you enter a value and submit then you will the entered value in second component we are passing the data from one component to another component with the help of BehaviorSubject. BehaviourSubject will return the initial value or the current value on Subscription.

Demo Githhub Stackblitz

Leave a Reply

Your email address will not be published. Required fields are marked *