dva组件,connect链接 models,所有的 state,或方法都通过 this.props获取

  1. function User(props) {}
  2. function mapStateToProps({ user, loading }) {
  3. return {
  4. ...user,
  5. // loading: loading.global,
  6. loading: loading.models.user
  7. };
  8. }
  9. function mapDispatchToProps(dispatch) {
  10. return {
  11. _$fetchUser: payload => dispatch({ type: 'user/fetchUser', payload }),
  12. };
  13. }
  14. export default connect(mapStateToProps, mapDispatchToProps)(User);

props.jpg

loading

  • loading.global 全局 loading
  • loading.models[namespace] 当前 models下的 loading
  • loading.effects[namespace/func] 当前请求的 loading

image.png

connect

  1. connect(TableList)()

image.png

@connect

装饰器语法

  1. @connect(state, dispatch)
  2. @connect(
  3. state => state.home,
  4. dispatch => {
  5. _@saveAction: payload => dispatch({type: 'home/list', paylod}),
  6. _@asyncTable: payload => dispatch({type: 'home/asyncTable', payload})
  7. }
  8. )