首页 技术 正文
技术 2022年11月21日
0 收藏 552 点赞 4,571 浏览 2980 个字

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. 目录结构

angular8 + redux 管理状态

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管理状态了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731