1. angular8.1.1 ----- package.json

{
"name": "angular-demo",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~8.1.1",
"@angular/common": "~8.1.1",
"@angular/compiler": "~8.1.1",
"@angular/core": "~8.1.1",
"@angular/forms": "~8.1.1",
"@angular/platform-browser": "~8.1.1",
"@angular/platform-browser-dynamic": "~8.1.1",
"@angular/router": "~8.1.1",
"redux": "^4.0.4",
"rxjs": "~6.4.0",
"tslib": "^1.9.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.801.1",
"@angular/cli": "~8.1.1",
"@angular/compiler-cli": "~8.1.1",
"@angular/language-service": "~8.1.1",
"@types/node": "~8.9.4",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "^5.0.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.4.3"
}
}

2. 目录结构

3. sate.js 导出default sate

export const basketballNums = [
    {
        id: '35',
        name: "杜兰特",
        age: '30',
        position: '前锋'
    }

4. action.js

action作为触发state 改变的唯一通道,type字段必须,payload字段传递参数,按需求可选。

export const ADD_NUMS = 'ADD_NUMS'
export const UPDATE_NUMS = 'UPDATE_NUMS'
export const DELETE_NUMS = 'DELETE_NUMS'
export function addItems(numObj) {
    return {
        type: 'ADD_NUMS',
        payload: numObj
    }
}

5. reducer.js -- 构造以action条件(type)为依据的函数,返回 state. --即制造state

import * as basketballActions from '../actions/bascketballActions'
import { basketballNums } from '../state/basketballState'
export function basketballReducer(state = basketballNums, action) {
    switch(action.type) {
        case basketballActions.ADD_NUMS: {
            return [...state, action.payload]
        }
       
        default:
            return state
    }
}

6. index.js -- 整合所有reducer

combineReducers -- 整合整合所有reducer

createStore -- 创建store, strore是一个 observalbal 对象,提供以下方法:

  • store.dispatch()
  • store.subscribe()
  • store.getState()
import { createStore, combineReducers } from 'redux'
import { basketballReducer } from './reducers/basketballReducers'
import { addItems } from './actions/bascketballActions'
export const allReducers = combineReducers({
    basketballState: basketballReducer
})
export const store = createStore(allReducers)
 
let unsubscribe = store.subscribe(()=>{
    console.log(store.getState())
})
store.dispatch(addItems({
    id: '0',
    name: '威少',
    position: '后卫',
    age: '30'
}))
unsubscribe()
 
7. angular组件中怎么引用?
 
import { Component } from '@angular/core';
import {store} from '../store'
import { addItems } from '../store/actions/bascketballActions'
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular-demo';
  constructor() {
    console.log(store, 'ss')
    store.dispatch(addItems({
            id: '11',
            name: '欧文',
            position: '后卫',
            age: '30'
        })
    )
    console.log(store.getState(), 'sss')
  }
}

以上7步, angular中就能用redux管理状态了。

angular8 + redux 管理状态的更多相关文章

  1. 使用Redux管理你的React应用

    因为redux和react的版本更新的比较频繁,博客园这里用的redux版本是1.0.1,如果你关心最新版本的使用技巧,欢迎来我的Github查看(https://github.com/matthew ...

  2. Redux管理你的React应用

    使用Redux管理你的React应用   因为redux和react的版本更新的比较频繁,博客园这里用的redux版本是1.0.1,如果你关心最新版本的使用技巧,欢迎来我的Github查看(https ...

  3. vuex 管理状态

    来分析下vuex的管理状态吧,如果你用过react中的redux的管理树,那我觉得vuex对你来说很容易掌握 如果你还是不太熟悉vuex是什么,那先看下官网https://vuex.vuejs.org ...

  4. 使用Redux管理React数据流要点浅析

    在图中,使用Redux管理React数据流的过程如图所示,Store作为唯一的state树,管理所有组件的state.组件所有的行为通过Actions来触发,然后Action更新Store中的stat ...

  5. 《图解HTTP》读书笔记(三:无状态协议/cookie管理状态)

    HTTP是一种不保存状态,即无状态(stateless)协议.HTTP协议自身不对请求和响应之间的通信状态进行保存. ——HTTP/1.1虽然是无状态协议,但为了实现期望的保持状态功能,于是引入了Co ...

  6. 前端(十):使用redux管理数据

    react本身能够完成动态数据的监听和更新,如果不是必要可以不适用redux. 安装redux: cnpm install redux --save,或者yarn add redux. 一.react ...

  7. React.js 小书 Lesson17 - 前端应用状态管理 —— 状态提升

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson17 转载请注明出处,保留原文链接和作者信息. 上一个评论功能的案例中,可能会有些同学会对一个 ...

  8. mysql管理----状态参数释义

    下面是数据库MySQL优化的一些步骤 一.通过show status和应用特点了解各种SQL的执行频率 通过SHOW STATUS可以提供服务器状态信息,也可以使用mysqladmin extende ...

  9. Redux 检测状态树变更

    一 概述 Redux只是检测引用是否改变. 如果状态树的某个值是对象.数组等,在reducer中需要生成一个新对象.新数组,才能被Redux检测到变更. let fruits = ['apple',' ...

随机推荐

  1. python中的内存机制

    首先要明白对象和引用的概念 (例子:a=1, a为引用,1为对象,对象1的引用计数器为1,b=1此时内存中只有一个对象1,a,b都为引用,对象的引用计数器此时为2,因为有两个引用) a=1,b=1 i ...

  2. 使用腾讯云提供的针对Nuget包管理器的缓存加速服务

    继阿里巴巴开源镜像站(https://opsx.alibaba.com/).华为云镜像站点(https://mirrors.huaweicloud.com/ )之后,腾讯也已于近日上线了类似的服务,官 ...

  3. 基于 HTML5 Canvas 的可交互旋钮组件

    前言 此次的 Demo 效果如下: Demo 链接:https://hightopo.com/demo/comp-knob/ 整体思路 组件参数 绘制旋钮 绘制刻度 绘制指针 绘制标尺 绘制文本 1. ...

  4. idea中写servlet时报错--关于405错误

    将super方法注释掉 原因:super是调用了此类继承父类doget和dopost方法的, 如果此类中没有这个方法,就会报错The specified HTTP method is not allo ...

  5. jQuery的核心思想

    jQuery?----www.jQuery.com jQuery的理念:write less, do more jQuery的成就:世界排名前100的公司,46%都在使用jQuery,远远超过其他库, ...

  6. Java匹马行天下之J2EE框架开发——Spring—>用IDEA开发Spring程序(01)

    一.心动不如行动 一.创建项目 *注:在IDEA中我创建的Maven项目,不了解Maven的朋友可以看我之前的博客“我们一起走进Maven——知己知彼”,了解Maven后可以看我之前的博客“Maven ...

  7. oracle 断电启动失败:ORA-00600: internal error code, arguments

    转载地址: http://www.2cto.com/database/201312/261602.html 由于服务器断电,启动 oracle 时报 ORA-00600 错误 查看 oracle tr ...

  8. table 表格 细边框 最简单样式

    table 表格细边框的效果实现方法虽然不难,但网上简单有效的方法却很少,在此记录一下 css 样式 /** table 细边框 **/ table{border-collapse: collapse ...

  9. Spring 5 新功能:函数式 Web 框架

    英文:ARJEN POUTSMA 译文:debugging, 达尔文, 混元归一, leoxu, xufuji456 链接:oschina.net/translate/new-in-spring-5- ...

  10. flask 使用基础

    转 https://blog.csdn.net/yelena_11/article/details/53404892