2016-04-08 89 views
5

新手問題:我有一個使用ngrx的angular2應用程序,我有一個服務將狀態(observables數組)返回給一個組件。在哪裏過濾狀態?

我的問題是我在哪裏過濾狀態,如果我想在組件中使用它的只讀子集?

我會在減速機,服務或組件中執行嗎?

回答

2

一些指導可以在ngrx example application找到。存在其中選擇器被定義alongside reducers一個圖案:

/** 
* Because the data structure is defined within the reducer it is optimal to 
* locate our selector functions at this level. If store is to be thought of 
* as a database, and reducers the tables, selectors can be considered the 
* queries into said database. Remember to keep your selectors small and 
* focused so they can be combined and composed to fit each particular 
* use-case. 
*/ 
export function getBookEntities() { 
    return (state$: Observable<BooksState>) => state$ 
    .select(s => s.entities); 
}; 

而這些選擇器是used in (smart) components選擇/篩選狀態:

... 
export class CollectionPage { 

    books$: Observable<BooksInput>; 

    constructor(store: Store<AppState>) { 
    this.books$ = store.let(getBookCollection()); 
    } 
} 

這種模式/機制可用於過濾的狀態下任組件或服務 - 以最適合您的架構爲準。

+0

好的,謝謝澄清 – user3253156