如何在Reducer中获取新添加的数据的ID

时间:2019-05-02 12:44:31

标签: angular redux ngrx

我正在调度一个动作,该动作将在数据库中创建数据,并将该数据添加到reducer中。我的问题是如何获取刚刚添加的数据的ID?我想向用户显示ID。

//on button click this method dispatch a action
create(){
 this.store.dispatch(new fromStore.CreateData({test:"hello"}));
 this.idOfCreatedData = ""; //dont know how
 //normally i retrieve data from store through 'selectors' like
 this.alldata= this.store.select(fromStore.getAllData);
} 

数据是在

之类的商店中创建的

id:{   测试:“你好” }

减速器具有这些标准选择器。

export function reducer():{ //just normal reducer
   switch(){
     case...
   }
}

export const getDataEntities= (state: MyState) => state.entities;
export const getDataLoading = (state: MyState) => state.loading;
export const getDataLoaded = (state: MyState) => state.loaded;
// here also, how to export just added data

外观与https://github.com/UltimateAngular/ngrx-store-effects-app/blob/15-create-pizza/src/products/store/reducers/pizzas.reducer.ts类似 和https://github.com/UltimateAngular/ngrx-store-effects-app/blob/15-create-pizza/src/products/store/selectors/pizzas.selectors.ts

我想要实现的是,当将数据添加到数据库中并且处于mystate状态时,我想在HTML上打印

your new data entry has been added and id for this entry is {{idOfCreatedData}}

1 个答案:

答案 0 :(得分:0)

调度动作后,化简器将值添加到商店中,您必须从商店中选择数据

this.store.select(yourSelectorFunction).subscribe()

我认为ngrx的官方示例应用程序是更好的资源: https://github.com/ngrx/platform/tree/master/projects/example-app