随笔分类 -  Rudux 快速使用教程

redux 是什么框架都可以用的,主要用于全局管理步骤 使用步骤 1 cnpm install redux --save import {createStore, combineReducers} from 'redux' import UserInfo from './userInfo' import List from './list' const rootReducer = combineReducers({ userInfo: UserInfo, list: List }); const store = createStore(rootReducer, {}); export default store; 创建两个模块,模块内容 export default function (state, action){ console.log('userInfo模块执行了'); console.log(state, action); //初始化userInfo的数据 if(!state){ return { name: '张三', age: 0 } } //修改名字事件 if(action.type == 'modify-name'){ return Object.assign(state, {name: action.value}); } else{ return state; } } 然后在页面操作 // 获取数据

{store.getState().name}

modifyName(){ let name = this.refs.name.value; // 修改数据的方法,出发action store.dispatch({ type: 'modify-name', value: name }) } addItem(){ let item = this.refs.item.value; store.dispatch({ type: 'add-item', value: item }) } }
摘要:redux 是什么框架都可以用的,使用步骤 1 cnpm install redux --save import {createStore, combineReducers} from 'redux' //引入redux 里面的方法 import UserInfo from './userInfo' 阅读全文
posted @ 2018-06-28 15:16 海底城 阅读(147) 评论(0) 推荐(0)