基础知识及示例,参考:
https://blog.csdn.net/chenqiuge1984/article/details/80130244
官方示例:
官方在线demo:
以下开始讲怎么做:
在项目里(的工具组件目录/公共目录)创建如下目录结构,
Models文件夹存放数据模型
Action 存放行为类型
Reducer 写具体的控制方法,
创建index.ts作为各个reducer的汇总,便于之后管理拓展。
Model 示例:
1 | export interface Book { |
2 | id: string; |
3 | volumeInfo: { |
4 | title: string; |
5 | subtitle: string; |
6 | authors: string[]; |
7 | publisher: string; |
8 | publishDate: string; |
9 | description: string; |
10 | averageRating: number; |
11 | ratingsCount: number; |
12 | imageLinks: { |
13 | thumbnail: string; |
14 | smallThumbnail: string; |
15 | }; |
16 | }; |
17 | } |
18 | |
19 | export function generateMockBook(): Book { |
20 | return { |
21 | id: '1', |
22 | volumeInfo: { |
23 | title: 'title', |
24 | subtitle: 'subtitle', |
25 | authors: ['author'], |
26 | publisher: 'publisher', |
27 | publishDate: '', |
28 | description: 'description', |
29 | averageRating: 3, |
30 | ratingsCount: 5, |
31 | imageLinks: { |
32 | thumbnail: 'string', |
33 | smallThumbnail: 'string', |
34 | }, |
35 | }, |
36 | }; |
37 | } |
Action 示例:
1 | import { Incident } from './../models/incidentModel'; |
2 | import { Action } from "@ngrx/store"; |
3 | |
4 | export const SAVE_INCIDENT = "SAVE_INCIDENT"; |
5 | export const RESET = "RESET"; |
6 | |
7 | export class SaveIncident implements Action { |
8 | readonly type = SAVE_INCIDENT; |
9 | constructor(public payload: Incident) {} |
10 | } |
11 | export class Reset implements Action { |
12 | readonly type = RESET; |
13 | } |
14 | |
15 | export type Actions = SaveIncident | Reset; |
reducer 示例:
1 | import * as inc from '../../action/incidentAction'; |
2 | import { Incident, generateMockIncident } from './../../models/incidentModel'; |
3 | |
4 | export function incidentReducer(incident: Incident = generateMockIncident(), action: inc.Actions) { |
5 | switch (action.type) { |
6 | case inc.SAVE_INCIDENT: |
7 | // return action.payload; |
8 | return Object.assign({}, incident, action.payload); |
9 | |
10 | case inc.RESET: |
11 | return generateMockIncident(); |
12 | |
13 | default: |
14 | return incident; |
15 | } |
16 | } |
Reducer –> index.ts: 多个模块集成到一起
1 | |
2 | import * as incident from './incident/incidentReducer'; |
3 | import { Incident } from '../models/incidentModel'; |
4 | |
5 | import * as incidentDetail from './incidentDetail/incidentReducer'; |
6 | import { IncidentDetail } from '../models/incidentDetailModel'; |
7 | |
8 | import * as initialKeyWord from './consultKeywordReducer'; |
9 | |
10 | export interface State { |
11 | incident: Incident; |
12 | incidentDetail: IncidentDetail; |
13 | initialKeyWord: string; |
14 | } |
15 | |
16 | export const reducer = { |
17 | incident: incident.incidentReducer, |
18 | incidentDetail: incidentDetail.incidentDetailReducer, |
19 | initialKeyWord: initialKeyWord.initialKeyWordReducer, |
20 | }; |
随后在app.modules.ts (声明组件的文件里)注入reducer:
1 | import {StoreModule} from '@ngrx/store'; |
2 | // ngrx reducer |
3 | import { incidentReducer } from './commonTools/ngrx/reducer/incident/incidentReducer'; |
4 | import { incidentDetailReducer } from './commonTools/ngrx/reducer/incidentDetail/incidentReducer'; |
5 | import { initialKeyWordReducer } from './commonTools/ngrx/reducer/consultKeywordReducer'; |
6 | ({ |
7 | imports: [ |
8 | StoreModule.provideStore({ |
9 | incident: incidentReducer, |
10 | incidentDetail: incidentDetailReducer, |
11 | initialKeyWord: initialKeyWordReducer, |
12 | // reducers: reducer, |
13 | }), |
14 | ], |
15 | }) |
随后在具体的组件中进行存取操作: 例如警情组件:
存数据步骤:
导入相关模块:
1 | import * as reducer from '../../../commonTools/ngrx/reducer/index'; |
2 | import { Store } from '@ngrx/store'; |
3 | import * as incAction from '../../../commonTools/ngrx/action/incidentAction'; |
constructor
1 | constructor( |
2 | private store: Store<reducer.State>, |
3 | ) { |
4 | } |
1 | // ngrx store 存数据, data为具体要存进去的数据 |
2 | this.store.dispatch({ |
3 | type: incAction.SAVE_INCIDENT, |
4 | payload: data |
5 | }); |
取数据步骤:
导入模块:
1 | import { Observable } from 'rxjs/Observable'; |
2 | import { Subscription } from 'rxjs/Subscription'; |
3 | import { Store } from '@ngrx/store'; |
4 | import { Incident, generateMockIncident } from '../../../commonTools/ngrx/models/incidentModel'; |
5 | import * as inc from '../../../commonTools/ngrx/reducer/index'; |
定义变量接收:
1 | currentInc$: Observable<Incident>; |
2 | private tagStateSubscription: Subscription; |
1 | constructor( |
2 | private store: Store<inc.State>, |
3 | ) { |
4 | this.currentInc$ = store.select('incident'); |
5 | } |
订阅:
1 | ngOnInit() { |
2 | this.tagStateSubscription = this.currentInc$.subscribe((incident) => { |
3 | this.info = incident; |
4 | }); |
5 | } |
组件销毁时取消订阅
1 | ngOnDestroy() { |
2 | this.tagStateSubscription.unsubscribe(); |
3 | } |
使用上大致就是这样,关于原理和概念都在开头的几个链接。
都是在摸索中前行。共勉。