gpt4 book ai didi

angular - BehaviourSubject 的 distinctUntilChanged() 不是函数

转载 作者:太空狗 更新时间:2023-10-29 17:08:15 25 4
gpt4 key购买 nike

我是 Rxjs 新手我正在尝试了解 BehaviourSubject下面是我的代码

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);
}
}

当我运行我的项目时,我在控制台中收到此错误

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

谁能帮帮我。另外,如果我想做的是为我的模型对象创建一个商店,那么如果有任何其他更简单的方法,请随时提出建议。

感谢任何帮助。

最佳答案

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

import 'rxjs/add/operator/distinctUntilChanged';

使用 Pipeable Operators 更新 rxjs > 5.5

import { distinctUntilChanged } from 'rxjs/operators';

Pipeable 运算符有助于构建和摇树。

要了解更多关于 benefits of Pipeable operators you may look in here 的信息.

希望这对您有所帮助!

关于angular - BehaviourSubject 的 distinctUntilChanged() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39667133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com