- 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. }
}
- 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`
})
}
})
}
}