MVVM中VM的作用
VM负责把Model的数据同步到View显示出来,还负责把View的修改同步修改回Model.
Redux
设计思想
1.Web应用就是一个状态机,视图和状态是一一对应的。
2.所有的状态,保存在一个对象里。
store
store是保存数据的地方,可以把它看成一个容器,一个应用只能有一个redux
redux提供createStore这个函数,来生成Store
import { createStore } from 'redux';
const store=createStore(fn);
1.当我们需要修改state时,我们需要发出一个action
store.dispatch(action);
2.store收到action需要作出相应的改变。称为reducer,reducer在createStore中作为参数传入
const defaultState = 0;
const reducer=(state=defaultState,action)={
switch(action.type){
case 'ADD_TO':
return state+action.payload;
default:state;
}
}
const store=createStore(reducer);
3.state改变后,需要view自动更新。使用store.suscribe(listener)进行监听。如果把render或者getState写入listener就能实现自动更新
1686

被折叠的 条评论
为什么被折叠?



