我正在我的angular2应用程序中实现ngrx存储,
使用两个对象
来存储减速器如下所示export function loadSuccessNamesAction(state: StoreData, action: loadSuccessNamesAction): StoreData {
const namesDataState = Object.assign({}, state);
namesDataState .isSpinner = false;
namesDataState .names = action.payload.names;
return namesDataState ;
}
export function loadSuccessSubjectAction(state: StoreData, action: loadSuccessSubjectAction): StoreData {
const subjectDataState = Object.assign({}, state);
subjectDataState .isSpinner = false;
subjectDataState .subject = action.payload.subject;
return namesDataState ;
}
在我的组件中,我想只获取namesDataState,所以我如何只能订阅商店中的一个对象。
目前我正在订阅整个商店,而不是我只想订阅第一个对象。
任何解决方案都会有所帮助,谢谢。
答案 0 :(得分:0)
this.store.select('namesDataState')
//Only emit when any name has changed
.distinctUntilKeyChanged(
//Key to filter on
'name',
//Optional comparison function if you have a specific way of comparing keys
(x, y) => x.name === y.name)
.pluck('name')
.subscribe((name: any) => {});