Type-Safe Selectors
The Problem with State Classes as Selectors
@Selector([ZooState])
static getAnimals(state: ZooStateModel) { ... }Using a StateToken
import { StateToken } from '@ngxs/store';
export const ZOO_STATE_TOKEN = new StateToken<ZooStateModel>('zoo');@State({ name: ZOO_STATE_TOKEN, defaults: { animals: [] } })
@Injectable()
export class ZooState {}
export class ZooSelectors {
// TypeScript now enforces that `state` is ZooStateModel
@Selector([ZOO_STATE_TOKEN])
static getAnimals(state: ZooStateModel) {
return state.animals;
}
}Wrapping an Existing State Class
Slicing State with createPropertySelectors
Selecting a Subset with createPickSelector
Building View Models with createModelSelector
Last updated

