2016-09-23 20 views
7

Tôi mới vào Rxjs tôi đang cố gắng hiểu BehaviourSubject dưới đây là mã của tôiBehaviourSubject của distinctUntilChanged() không phải là một chức năng

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

Khi tôi chạy dự án của tôi sau đó tôi nhận được lỗi này trong giao diện điều khiển của tôi

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 

Có ai có thể giúp tôi không. Ngoài ra nếu tôi đang cố gắng làm là để tạo ra một cửa hàng cho các đối tượng mô hình của tôi vì vậy nếu có bất kỳ cách nào đơn giản khác cảm thấy tự do để đề nghị nó.

Mọi trợ giúp đều được đánh giá cao.

Trả lời

23

bạn phải nhập toàn bộ thư viện rxJs hoặc thư viện cụ thể cho thư viện này.

import 'rxjs/add/operator/distinctUntilChanged'; 

Cập nhật rxjs> 5,5 với Lettable nhà khai thác,

import { distinctUntilChanged } from 'rxjs/operators'; 

khai thác lettable giúp xây dựng và cây run rẩy.

Để tìm hiểu thêm trên benefits of lettable operators you may look in here.

Hy vọng điều này sẽ giúp ích !!

+0

Xin chào, bạn có thể giúp tôi, tôi có webstorm và khi tôi đang sử dụng Trình điều khiển dễ sử dụng, Webstorm giữ chúng dưới dạng nhập "không sử dụng" –

1

Bạn thực sự phải nhập tất cả toán tử (đó là dodistinctUntilChanged) và BehaviorSubject.

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

Xem plnkr demo: http://plnkr.co/edit/Wbqv95EiG8BnzC8BpD7E?p=preview

Btw, tôi phải cẩn thận với những câu như private _store = _store vì nó làm cho nó rất khó đọc mặc dù nó làm những gì bạn muốn.

Điều này được tạo từ 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; 
}); 
Các vấn đề liên quan