2016-09-23 12 views
7

ではない私は、私は以下のBehaviourSubject を理解しようとしていますRxjs に新しいです私のコードは、私は私のプロジェクトを実行すると、私はBehaviourSubjectのdistinctUntilChanged()関数

私のコンソールにこのエラーが出ます

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 

誰でも私を助けることができます。また、私がしようとしているのは、私のモデルオブジェクト用のStoreを作成することです。他の簡単な方法があれば、それを提案することができます。

何か助けていただければ幸いです。

答えて

23

rxJsライブラリ全体または特定のものをインポートする必要があります。

import 'rxjs/add/operator/distinctUntilChanged'; 

更新rxjs賃貸可能演算子と> 5.5

import { distinctUntilChanged } from 'rxjs/operators'; 

賃貸可能事業者は、建物や木が揺れに役立ちます。

benefits of lettable operators you may look in hereについての詳細は、こちらをご覧ください。

これが役立ちますように!

+0

こんにちは、私はWebstormを持っていて、私がLettable Operatorsを使用しているとき、Webstormはそれらを「未使用」のインポートとして保持しています。 –

1

実際にはすべての演算子(つまり、dodistinctUntilChanged)とBehaviorSubjectもインポートする必要があります。

import 'rxjs/add/operator/distinctUntilChanged'; 
import 'rxjs/add/operator/do'; 
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; 

がplnkrデモを参照してください:ところでhttp://plnkr.co/edit/Wbqv95EiG8BnzC8BpD7E?p=preview

をそれはそれはあなたが何をしたいんが読んで、それは非常に困難になるので、私はそのようなprivate _store = _storeなどのステートメントに注意してくださいと思います。

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; 
}); 
関連する問題