博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[AngularJS Ng-redux] Integrate ngRedux
阅读量:6001 次
发布时间:2019-06-20

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

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());  }

 

转载地址:http://bmbmx.baihongyu.com/

你可能感兴趣的文章
mysql树状数据的数据库设计
查看>>
JavaScript快速入门
查看>>
Intger 自动装拆箱
查看>>
html中a连接触发表单提交
查看>>
Linux网卡名改eth0方法
查看>>
SQL or NoSQL——云计算环境中该选择谁
查看>>
托盘气泡很长时间才能消失,uTimeout没起到作用的解决办法
查看>>
利用webshell搭建socks代理
查看>>
nginx+keepalived构建主主负载均衡代理服务器
查看>>
10 分钟简读经典著作 Data Warehouse Toolkit
查看>>
屏蔽nginx以及php版本信息方法
查看>>
linux 命令详解 二十
查看>>
C语言嵌入系统编程修炼-性能优化
查看>>
java之数据库相关
查看>>
IKE野蛮模式及NAT穿越
查看>>
beego高级编程---->grace模块热重启导致旧进程未处理完请求直接退出
查看>>
.U盘插入电脑提示未知的USB设备unkown device
查看>>
洛谷—— P1605 迷宫
查看>>
PHP中的数组(Array)
查看>>
win7配置FTP
查看>>