@ngrx/entity
The demo code for this section can be found in the code repository for this book under Chapter10/Entity
.
The entity library is here to help us manage collections and basically, write less and do more. This means that so far we have been writing a lot of code when creating reducers and selectors that we simply won't need to do when we leverage the full power of the entity library.
Setting it up
We start by downloading the entity library. To do this, we need to run the following command:
npm install @ngrx/entity
We then need to perform the following steps:
- Create a model.
- Create an entity state based on the model.
- Create an entity adapter.
- Create the initial state.
- Create the reducer and set up the state in the
StoreModule.
Let's start off by creating our model:
// user.model.ts export interface User { id: number; name: string; }
The preceding code is just a simple model with fields id
and name
. We then create our entity state, like so:
// excerpt from app.module.ts import { ...