puddlejumper26 / blogs

Personal Tech Blogs
4 stars 1 forks source link

Basic Concepts of NgRx #175

Open puddlejumper26 opened 4 years ago

puddlejumper26 commented 4 years ago

NgRx

Courses

前五分钟解释 大概的使用流程 https://www.bilibili.com/video/BV1iJ411F7Bf?p=22

NGRX课程 https://www.bilibili.com/video/BV1nt4y127iP?p=3

简介 https://hijiangtao.github.io/2020/05/08/Angular-State-Management-Invest-Report/

puddlejumper26 commented 4 years ago

What is NgRx

image

NGRX Store 工作原理

第一次触发 Keep the data in the one way data flow

image

第二次触发,没有改变状态的情况下,就直接从Store发给组件了

image

什么时候使用 NgRx

不是什么都可以放入Store

puddlejumper26 commented 4 years ago

Difference between NgRx and normal Service

image

简单很多,不用再为一个组件写很多的服务

=========================

Back To Home

puddlejumper26 commented 4 years ago

Redux Priciples

image

image

puddlejumper26 commented 4 years ago

Reducer VS Multiple Reducers (Different States)

image

image

Reducer VS Multiple Reducers (break State further into small sub-slice)

image

image

puddlejumper26 commented 4 years ago

3

puddlejumper26 commented 4 years ago

4

puddlejumper26 commented 4 years ago

5

puddlejumper26 commented 4 years ago

2.0 整体的步骤

0.1 ng add@ngrx/store - 安装ngrx/store插件 -

0.2 ng add @ngrx/store-devtools- 调试插件

1.0 首先需要在相关的文件夹下面建立一个 xxx.module.ts 的文件

因为需要进行注册.一般都是 ng g m store 建立一个 store.module.ts . 这个APP中的名字是 index.ts 但是导出的module名字是 AppStoreModule

2.0 然后在平行的位置建立actions,reducers,selectors的文件夹,之后放入相关的ts文件

3.0 actions

4.0 reducers 是用来执行 actions中的动作

//下面的这个会在xxx.module.ts文件中import export function playerReducer(state: PlayState, action: Action){ return reducer(state, action); }


### 5.0  `selectors`
- 拿到 state 里的所有的数据
- 通过`createSelector`方法创建
```ts
// 第二个参数是一个函数,获取playing数据的selector
export const getPlaying = createSelector(selectPlayerStates, (state: PlayState) => state.playing);

6.0 xxx.module.ts

7.0 在最终的组件中进行使用

8.0 Redux 插件