NISHANT DESAI NISHANT DESAI - 2 months ago 8
TypeScript Question

BehaviourSubject's distinctUntilChanged() is not a function

I am new to Rxjs
I am trying understand BehaviourSubject
below is my code

export interface State {
items: Items[]
}

const defaultState = {
items: []
};

const _store = new BehaviorSubject<State>(defaultState);

@Injectable()
export class Store {
private _store = _store;
changes = this._store.distinctUntilChanged()
.do(() => console.log('changes'));

setState(state: State) {
this._store.next(state);
}

getState() : State {
return this._store.value;
}

purge() {
this._store.next(defaultState);
}
}


When i run my project then i get this error in my console

platform-browser.umd.js:1900 EXCEPTION: Error: Uncaught (in promise):
EXCEPTION: Error during instantiation of Store! (StoreHelper -> Store).
ORIGINAL EXCEPTION: TypeError: this._store.distinctUntilChanged is not a function


Can anyone help me out. Also if I am trying to do is to create a Store for my model objects so if there is any other simpler way feel free to suggest it.

Any help is appreciated.

Answer

you have to import entire rxJs library or the specific one for this.

import 'rxjs/add/operator/distinctUntilChanged';

Hope this helps!!