1. The effect picture after the case is running
Second, ngrx
the knowledge about
- 1. Official website address
- 2. It
ngrx
isredux
a thinking for reference ,angular
a state management package customized for China, similar to thereact
middleredux
and thevue
middlevuex
, mainly including the following modules (this article introduces first@ngrx/store
)@ngrx/store
@ngrx/store-devtools
@ngrx/effects
@ngrx/router-store
@ngrx/entity
@ngrx/data
@ngrx/schematics
- 3.
ngrx
The scene that needs to be usedangular
Not necessary in project development- Component communication and data sharing are required in large projects
Three, build the project
-
1. Use the
@angular/cli
initialization projectng new angular-ngrx
-
2. Create a data
module
(manually modify the nameAppStoreModule
, otherwise it will@ngrx/store
have the same name as in)ng g m store
-
3.
store
Create three folders under the folderactions
reducers
selectors
(Not necessary, only when a state tree is an object, write a method to select a node in the state tree)
-
4. Manual installation
@ngrx/store
npm install @ngrx/store --save
-
5. Manual installation
@ngrx/store-devtools
npm install @ngrx/store-devtools --save
-
6,
reducers
create under the folderindex.ts
(useng add @ngrx/store
will be generated by default)import { ActionReducerMap, MetaReducer } from '@ngrx/store'; import { environment } from '../../../environments/environment'; // 项目中全部的状态 export interface State {} // 全部的reducer函数 export const reducers: ActionReducerMap<State> = {}; export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
-
7. The browser needs to install
redux
plug-ins -
8. For more configuration
store.module.ts
of configuring browser debugging in@NgModule({ declarations: [], imports: [ StoreModule.forRoot(reducers, { metaReducers, runtimeChecks: { strictStateImmutability: true, strictActionImmutability: true, strictStateSerializability: true, strictActionSerializability: true, } }), StoreDevtoolsModule.instrument({ maxAge: 20, logOnly: environment.production }) ] }) export class AppStoreModule { }
Four, use in the project@ngrx/store
- 1. For the use of code, see the components in github
book