umi&&dva

  1. dva中通过connect绑定了路由组件
export default connect(({ myCustomer, global }) => ({
  advanceFilterJson: myCustomer.advanceFilterJson,
  objectDictionary: myCustomer.objectDictionary,
  dictionary: global.dictionary,
  authorities: global.authorities,
  userBasicInfo: global.userBasicInfo,
}))(MoreCondition);

2.通过reducer返回的新引用来更新store中的数据,第一个参数为preState,第二个参数为action对象

    namespace: 'monthCard',
    state: {
        list: [],
        total: 0,
        editData: {},
        loading: false
    },
    subscriptions: {},
    effects: {},
    reducers: {
 1.       updateState(state, action){
 2.           return {
 3.               ...state,
 4.               ...action.payload
 5.           }
 6.       }
    }
  1. effects用来处理异步操作,第一个参数为action,第二个参数是一些api,如call,put等。call用来调用异步方法,put是直接调用reducer中的方法。
+   //异步请求
    //request 是我们封装的一个网络请求库
+   async function queryFromService(data) {
+       return request("queryFromApi", {
+           data,
+           method: "post",
+           dataType: "payload"
+       })
+   }

    namespace: 'monthCard',
    state: {
        list: [],
        total: 0,
        editData: {},
        loading: false
    },
    subscriptions: {},
    effects: {
+       * query({ payload }, { call, put }){
+           yield put ({
+               type: `updateState`,
+               payload: {
+                   loading: true
+               }
+           })
+
+           const { data } = yield call (queryFromService, parse(payload))
+
+           if(data){
+               yield put({
+                   type: 'querySuccess',
+                   payload: {
+                       loading: false,
+                       list: data.data,
+                       editData: data.data,
+                       total: data.recordsTotal
+                   }
+               })
+           } else {
+               yield put({
+                   type: 'querySuccess',
+                   payload: {
+                       data: {},
+                       loading: false
+                   }
+               })
+           }       
+       },
+       * create(){},
+       * 'delete'(){},//delete是关键字
+       * update(){}
        
    },
    reducers: {
        updateState(state, action){
            return {
                ...state,
                ...action.payload
            }
        },

+       querySuccess(state, action){
+           if(action.payload.data){
+               const {
+                   data: {
+                       status,
+                       message
+                   }
+               } = action.payload
+
+               if(1 == status){
+                   //
+               }else {
+                   Message.error(message)
+               }
+           }
+
+           return {
+               ...state
+           }
+       }       
    }

4.路由组件connect后会props中会绑定dispatch方法,传递action参数,包括type和payload,根据type匹配effects和reducers中对应的方法,从而更新store中的值。
5.subscriptions还可以用来订阅数据源。

subscriptions: {
    setup(( dispatch, history )){
        history.listen(({ pathname, query }) => {
            if(pathToRegexp(`/y/monthCard/list`).test(pathname)) {
                dispatch({
                    type:`query`
                })
            }
        })
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值