[AngularJS Ng-redux] Integrate ngRedux
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());
}
[AngularJS Ng-redux] Integrate ngRedux的更多相关文章
- 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 ...
- part 4 AngularJS ng src directive
- Part 15 AngularJS ng init directive
The ng-init directive allows you to evaluate an expression in the current scope. In the following e ...
- [AngularJS] AngularJS系列(1) 基础篇
目录 什么是AngularJS? 为什么使用/ng特性 Hello World 内置指令 内置过滤器 模块化开发 一年前开始使用AngularJS(以后简称ng),如今ng已经出2了.虽说2已完全变样 ...
- 从angularJS看MVVM
javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到angularJS[ng]这么重量级的 ...
- angularJS看MVVM
从angularJS看MVVM javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到a ...
- js架构设计模式——从angularJS看MVVM
javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到 angularJS[ng] 这么重量 ...
- .Net Core应用框架Util介绍(一)
距离上次发文,已经过去了三年半,这几年技术更新节奏异常迅猛,.Net进入了跨平台时代,前端也被革命性的颠覆. 回顾 2015年,正当我还沉迷于JQuery + EasyUi的封装时,突然意识到技术已经 ...
- gulp 在 angular 项目中的使用
gulp 在 angular 项目中的使用 keyword:gulp,angularjs,ng,ngAnnotate,jshint,gulpfile 最后附完整简洁的ng项目gulpfile.js 准 ...
随机推荐
- POJ 4047 Garden
Garden Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 404 ...
- [ES6] Extends class in ES6 vs ES5 subclass
ES6 class with extends and super: class Tree { constructor(size = ', leaves = {spring: 'green', summ ...
- 分享一些 WINDOWS 资源站点(备用)
分享一些 WINDOWS 资源站点(备用) Windows Embedded http://www.10down.net/windows-embedded.php Windows 7 SP1 Upd ...
- js20---接口3种方式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- BZOJ5394: [Ynoi2016]炸脖龙(欧拉广义降幂)
就是让你求这个: 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5394 解题思路: NOIP2018后第一道题,感觉非常像那个上帝与集合的 ...
- UML类图与类图中的关系
以下内容摘自<Java与模式> 在类与类之间,会有连线指明它们之间的关系.类和类.类和接口.接口和接口之间可以建立以下几种关系:一般化关系.关联关系.聚合关系.合成关系和依赖关系,这几种关 ...
- JS实践与写博客-序
大二的时候,就开始接触JavaScript了. 当时学了1年多,主要是认真看了一本JavaScript的入门书籍,了解了JavaScript大致怎么回事.在独自做Web项目的时候,用的都是JavaSc ...
- HDU——T 3579 Hello Kiki
http://acm.hdu.edu.cn/showproblem.php?pid=3579 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- 1.2 Use Cases中 Event Sourcing官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Event Sourcing 事件采集 Event sourcing is a st ...
- Python的数组合并
https://blog.csdn.net/hustqb/article/details/78090365 TypeError: can only concatenate list (not &quo ...