Up to this point, we have created an effective, yet rudimentary, implementation of Redux by manually creating an application store, reducers and action creators. We have already made significant progress in simplifying state management within our application, but we have in fact only touched the surface of what Redux has to offer.

In this lesson, we are going to introduce ngRedux into our application and let it handle the rest of the heavy lifting as we work through this series. ngRedux is a project that provides angular bindings to redux to streamline integrating it into our application.

Install:

npm install --save redux ng-redux

Config:

import {categories, initialCategories, CategoriesActions} from './components/categories/category.state';
// import Store from './app.store';
import ngRedux from 'ng-redux';
//const store = new Store(categories, initialCategories);
const config = $ngReduxProvider => {
'ngInject';
// createStoreWith(1p, 2p, 3p, 4p)
//1p: reducer
//2P: middleware
//3p: enhancer
//4p: initial state
$ngReduxProvider.createStoreWith(categories, [], [], initialCategories);
};

Previosuly, we use the stroe class we created, now we replace with ng-redux.

Using:

constructor($timeout, CategoriesActions, $ngRedux) {
'ngInject'; angular.extend(this, {
$timeout,
CategoriesActions
}); this.store = $ngRedux;
}

In the controller, we can inject '$ngRedux' and assign it to stroe.

Then we can use it as the same before.

  $onInit() {
this.unsubscribe = this.store.subscribe(() => {
this.categories = this.store.getState();
}); this.store.dispatch(this.CategoriesActions.getCategoreis()); this.$timeout(( )=> {
const data = [
{id: 0, name: 'Angular'}
];
this.store.dispatch(this.CategoriesActions.getCategoreis(data));
}, 2000);
}

More than often we need to deal with multi reducers in our app.  So we need to combine those reducers to make it easy to use.

Import:

import { categories, initialCategories, CategoriesActions, category } from './components/categories/category.state';
import { combineReducers } from 'redux';
const rootReducers = combineReducers({
categories,
category
});

Then we can pass the 'rootReducer' to createStoreWith() function:

const config = $ngReduxProvider => {
'ngInject';
$ngReduxProvider.createStoreWith(rootReducers, []);
};

Now it affects how to getState() function used, now the function return our an object which container both 'categories' and 'category' state.

  $onInit() {
this.unsubscribe = this.store.subscribe(() => {
this.categories = this.store.getState().categories;
this.currentCategory = this.store.getState().category;
}); this.store.dispatch(this.CategoriesActions.getCategoreis());
}

Github

[AngularJS Ng-redux] Integrate ngRedux的更多相关文章

  1. Part 6 AngularJS ng repeat directive

    ng-repeat is similar to foreach loop in C#. Let us understand this with an example. Here is what we ...

  2. part 4 AngularJS ng src directive

  3. Part 15 AngularJS ng init directive

    The ng-init directive allows you to evaluate an expression in the current scope.  In the following e ...

  4. [AngularJS] AngularJS系列(1) 基础篇

    目录 什么是AngularJS? 为什么使用/ng特性 Hello World 内置指令 内置过滤器 模块化开发 一年前开始使用AngularJS(以后简称ng),如今ng已经出2了.虽说2已完全变样 ...

  5. 从angularJS看MVVM

    javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到angularJS[ng]这么重量级的 ...

  6. angularJS看MVVM

    从angularJS看MVVM   javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到a ...

  7. js架构设计模式——从angularJS看MVVM

    javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到 angularJS[ng] 这么重量 ...

  8. .Net Core应用框架Util介绍(一)

    距离上次发文,已经过去了三年半,这几年技术更新节奏异常迅猛,.Net进入了跨平台时代,前端也被革命性的颠覆. 回顾 2015年,正当我还沉迷于JQuery + EasyUi的封装时,突然意识到技术已经 ...

  9. gulp 在 angular 项目中的使用

    gulp 在 angular 项目中的使用 keyword:gulp,angularjs,ng,ngAnnotate,jshint,gulpfile 最后附完整简洁的ng项目gulpfile.js 准 ...

随机推荐

  1. IFC数据模式架构的四个概念层

    IFC模型体系结构由四个层次构成, 从下到上依次是 资源层(Resource Layer).核心层(Core Layer).交互层(Interoperability Layer).领域层(Domain ...

  2. ActiveMQ学习总结(7)——ActiveMQ使用场景

    MQ简介: MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们.消息传 ...

  3. Android 多线程下载,断点续传,线程池

    你可以在这里看到这个demo的源码: https://github.com/onlynight/MultiThreadDownloader 效果图 这张效果图是同时开启三个下载任务,限制下载线程数量的 ...

  4. Monkey脚本编写

    脚本优势:简单.快捷.不需要借助任何工具,可以做简单的性能测试 脚本缺点:只能简单实现坐标.按键等基本操作,无逻辑性 脚本格式:

  5. Reentrant 可重入解释

    作者:知乎用户链接:https://www.zhihu.com/question/37168009/answer/88086943来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  6. BZOJ4817: [Sdoi2017]树点涂色(LCT)

    Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...

  7. Maven搭建hadoop环境报Missing artifact jdk.tools:jdk.tools:jar:1.7(5种办法,2种正解)

    刚刚写的那一篇,是网上比较主流的解决办法. 鉴于实际情况,有伙伴的机器上没有遇到这个问题,我们再探究原因,最终还有4种情况需要说明. 先说,另外一种"正解". <depend ...

  8. Spring学习总结(11)——Spring JMS MessageConverter介绍

    消息转换器MessageConverter MessageConverter的作用主要有两方面,一方面它可以把我们的非标准化Message对象转换成我们的目标Message对象,这主要是用在发送消息的 ...

  9. git还原文件

    http://jingyan.baidu.com/album/e4511cf33479812b855eaf67.html

  10. 洛谷——P1042 乒乓球

    https://www.luogu.org/problem/show?pid=1042 题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改 ...