博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[AngualrJS NG-redux] Map State and Dispatchers to Redux
阅读量:5261 次
发布时间:2019-06-14

本文共 2695 字,大约阅读时间需要 8 分钟。

In this lesson, we are going to learn how to map our Angular component directly to our application store using the connect method on ngRedux.

In Angular, it is a common technique to bind a controller property directly to the model so that our controllers remain lightweight and operate more as a pass through mechanism than anything else.

Using the connect method, we can accomplish the same effect by not only binding properties directly to the application store, but also binding our controller to our action creators. This allows us to drastically simplify our controllers as we are able to delete a bunch of local controller methods as they become unnecessary because of our mappings.

 

connect(mapStateToThis, actions)(context):

p1: mapStateToThis: function(state),

p2: actions: object --> ActionsCreators

p3: Context

 

Basiclly connect do two things:

1. Exports the actions to the controller,  so you don't need to do like this:

this.store.dispatch(this.CategoriesActions.getCategoreis());

Instead you can do :

this.getCategoreis();

 

2. You don't need to subscribe to the store manully anymore, it will automaticlly subscribe to store:

So instead doing like this:

this.unsubscribe = this.store.subscribe(() => {      this.categories = this.store.getState().categories;      this.currentCategory = this.store.getState().category;    });

Now you can do this:

this.unsubscribe = this.store.connect(this.mapStateToThis, actions)(this);

The connect function also return a unsubscribe function which you can call in $onDestroy life cycle.

 

mapStateToThis:

mapStateToThis(state) {    return {      categories: state.categories,      currentCategory: state.currentCategory    };  }

Basiclly it will replace:

this.unsubscribe = this.store.subscribe(() => {      this.categories = this.store.getState().categories;      this.currentCategory = this.store.getState().category;    });

 

actions:

const actions = Object.assign({}, this.CategoriesActions, this.BookmarksActions);    this.unsubscribe = this.store.connect(this.mapStateToThis, actions)(this);

Actions is an object contains the actions creators you need for this controller.

 

Other benefits:

So for example you have a function called 'deleteBookmark' on the controller. And inside this function, you call 'this.BookmarksActions.deleteBookmark(bookmark)' function.

Two functions' name are the same, then you can get rid of 'deleteBookmark' the whole function.

/*// The whole function can be removeddeleteBookmark(bookmark) {    this.store.dispatch(      this.BookmarksActions.deleteBookmark(bookmark)    )  }*/

Becasue connect function already exprots the 'deletedBookmark' function to the ctrl.

转载于:https://www.cnblogs.com/Answer1215/p/6063087.html

你可能感兴趣的文章
安装NVIDIA驱动时禁用自带nouveau驱动
查看>>
HDU-1255 覆盖的面积 (扫描线)
查看>>
css3学习01
查看>>
【USACO】 奶牛会展
查看>>
ActiveMQ笔记之点对点队列(Point-to-Point)
查看>>
继承和多态
查看>>
Dijkstra+计算几何 POJ 2502 Subway
查看>>
修复IE不能执行JS的方法
查看>>
程序员究竟该如何提高效率zt
查看>>
希尔排序法(缩小增量法)
查看>>
PHP编程基础学习(一)——数据类型
查看>>
MongoDB-JAVA-Driver 3.2版本常用代码全整理(2) - 查询
查看>>
NPOI处理Word文本中上下角标
查看>>
Android笔记 Handler
查看>>
如何阅读大型前端开源项目的源码(转)
查看>>
java.util.Arrays类详解
查看>>
idea搭建tocmat
查看>>
NYOJ-626-intersection set(二分查找)
查看>>
项目管理之路(1):初步踏入项目管理
查看>>
Java 中 静态方法与非静态方法的区别
查看>>