typescript BehaviourSubject 的 distinctUntilChanged() 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39667133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BehaviourSubject's distinctUntilChanged() is not a function
提问by Nishant Desai
I am new to Rxjs I am trying understand BehaviourSubject below is my code
我是 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);
}
}
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.
谁能帮我吗。此外,如果我想做的是为我的模型对象创建一个 Store,那么如果有任何其他更简单的方法,请随时提出建议。
Any help is appreciated.
任何帮助表示赞赏。
回答by Madhu Ranjan
you have to import entire rxJs library or the specific one for this.
您必须为此导入整个 rxJs 库或特定的库。
import 'rxjs/add/operator/distinctUntilChanged';
Update rxjs > 5.5with Pipeable Operators,
使用可管道操作符更新 rxjs > 5.5,
import { distinctUntilChanged } from 'rxjs/operators';
Pipeable operators helps building and tree shaking.
可管道操作符有助于构建和摇树。
To learn more on the benefits of Pipeable operators you may look in here.
要了解有关Pipeable 运算符的好处的更多信息,您可以查看此处。
Hope this helps!!
希望这可以帮助!!
回答by martin
You actually have to import all operators (that's do
and distinctUntilChanged
) and the BehaviorSubject
as well.
您实际上必须导入所有运算符(即do
和distinctUntilChanged
)以及 the BehaviorSubject
。
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/do';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
See plnkr demo: http://plnkr.co/edit/Wbqv95EiG8BnzC8BpD7E?p=preview
参见 plnkr 演示:http://plnkr.co/edit/Wbqv95EiG8BnzC8BpD7E?p=preview
Btw, I'd be careful with statements such as private _store = _store
because it makes it very hard to read even though it does what you want.
顺便说一句,我会小心这样的陈述,private _store = _store
因为即使它做了你想要的,它也很难阅读。
This is generated from https://www.typescriptlang.org/play/.
这是从https://www.typescriptlang.org/play/生成的。
define(["require", "exports"], function (require, exports) {
"use strict";
var _store = new BehaviorSubject(defaultState);
var Store = (function () {
function Store() {
this._store = _store;
this.changes = this._store.distinctUntilChanged()
.do(function () { return console.log('changes'); });
}
Store.prototype.setState = function (state) {
console.log(_store);
this._store.next(state);
};
Store.prototype.getState = function () {
return this._store.value;
};
Store.prototype.purge = function () {
this._store.next(defaultState);
};
return Store;
}());
exports.Store = Store;
});