BehaviourSubject的distinctUntilChanged()不是函数 [英] BehaviourSubject's distinctUntilChanged() is not a function

查看:114
本文介绍了BehaviourSubject的distinctUntilChanged()不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rxjs的新手 我正在尝试了解行为主题 下面是我的代码

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.

感谢您的帮助.

推荐答案

您必须为此导入整个rxJs库或特定的库.

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

import 'rxjs/add/operator/distinctUntilChanged';

使用管道运算符更新rxjs> 5.5

import { distinctUntilChanged } from 'rxjs/operators';

可操作的运算符有助于建筑物和树木的摇晃.

Pipeable operators helps building and tree shaking.

要了解有关 Pipeable运算符的好处的更多信息,在这里.

希望这会有所帮助!

这篇关于BehaviourSubject的distinctUntilChanged()不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆