Sidenav Actions
Let's create some actions that will toggle the visibility of a sidenav.
Open the src/app/store/actions.ts file.
SidenavActionTypes enum
To get started, create an enum with each action's type string:
export enum SidenavActionTypes {
HideSidenav = '[Sidenav] Hide Sidenav',
ShowSidenav = '[Sidenav] Show Sidenav'
}Classes
Next, create two classes:
- HideSidenav
- ShowSidenav
export class HideSidenav implements Action {
readonly type = SidenavActionTypes.HideSidenav;
}
export class ShowSidenav implements Action {
readonly type = SidenavActionTypes.ShowSidenav;
}- Each class implements the
Actioninterface that we defined. - Each class as a
typeproperty that is associated with the string literal from theSidenavActionTypesenum.
Type Union
Finally, export a new type that is the union of the two classes:
export type SidenavAction = HideSidenav | ShowSidenav;This will enable us to specify the action type when defining the reducer function for the sidenav state.