weixin_44058725
雨生百谷,方为谷雨
采纳率100%
2019-09-26 10:23

dva.js怎么异步获取接口数据

已采纳

这个是components里点击按钮,出发dispatch

 let p=feature.properties;
   let SmID=p.SmID;
  this.querySettlementPoint(1);//提交了一个effects
  let {SettlementPointData}=this.props.mapindex;//立即打印返回的数据是空的
  console.log(SettlementPointData)//结果为空
 setTimeout(()=>{
  let {SettlementPointData}=this.props.mapindex;//1000ms后打印
  console.log(SettlementPointData)//结果是有值的
 },1000)
 querySettlementPoint = (...payload) => { //获取沉降点详情
    // console.log(payload)
    this.props.dispatch({
      type: 'mapindex/querySettlementPoint',
      payload: {
        ids:payload
      }
    })
  }
```这个是moudels里effects


import { queryHttp,exportHttp,reqResult } from '../service/service'
import { apiUrl } from '../../public/config'
export default {
namespace: 'mapindex',

state: {
    clearMapId:'',
    cjListQueryShow:false,
    SettlementPointData:"",//沉降点详情
},

reducers: {
    setState(state, { payload }) {
      // console.log(payload,333,state)
        return { ...state, ...payload};
    },
},

effects: {
  *querySettlementPoint({payload},{call,put}){//沉降点详情接口
    const result = yield call(queryHttp, apiUrl.querySettlementPoint,payload)
    const res = reqResult(result.data);
    yield put({
        type: 'setState',
        payload: {
            'SettlementPointData':res.list[1]
        }
    })
},
},

subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line

    },
},

};

问题就是在render里能拿到值,但是在render外拿不到,直接打印为空,但是1秒后打印就有了,我不想用定时器(接口响应事件很长),还有别的更好的方式吗?
我现在的疑问就是,在render里能拿到返会的值,我不想在render里用,想直接在点击之后就拿到并且操作,该怎么办。
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答