I am using Angular 2.
When I use either of these two, my program runs well:
import { BehaviorSubject } from 'rxjs/Rx'; import { BehaviorSubject } from 'rxjs';
However, I try to use the following way:
import { BehaviorSubject } from 'rxjs/subject/BehaviorSubject';
But I failed, my browser console shows:
Uncaught Error: Cannot find module 'rxjs/subject/BehaviorSubject'
How can I use third way correctly? Thanks
To solve the error "Cannot find module 'rxjs-compat/Observable'", make sure to install the package by opening your terminal in your project's root directory and running the following command: npm i rxjs and restart your IDE and development server. Copied!
BehaviorSubject is a variant of a Subject which has a notion of the current value that it stores and emits to all new subscriptions. This current value is either the item most recently emitted by the source observable or a seed/default value if none has yet been emitted.
1. First create a BehaviorSubject in order service which holds the initial state of order count ,so that it can be used by any component. 2. Now all observers(3 components) need to subscribe to source observable to get current value and show it on UI.
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
rxjs 6.x
import {BehaviorSubject} from 'rxjs';
See also
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With