1.安装redux 使用 npm install -save redux 安装redux,注意使用-save 表示安装到依赖中;
2. 创建store文件夹,下面创建 index.js 和 reducer.js文件;
index.js:
import { createStore } from 'redux'; import reducer from './reducer'; const store = createStore(reducer); export default store;
reducer.js
const defaultState ={ inputValue:'默认信息', list:[] }; export default (state = defaultState ,action)=>{ return state; }
在html中调用:
import store from './store'
constructor(props){ super(props); this.state = store.getState();//redux 提供的获取数据的方法 console.log(this.state); }