dva数据流effects获取返回值的几种方式

  • Post author:
  • Post category:其他


1、传递callback回调

// models
effects: {
  *add({ payload, callBack }, { call, put, select }) {  // eslint-disable-line
      const response = yield request('/apis/serverless/add', {
          method: 'GET',
          data: payload,
      });
      const num = yield select(state => state.num + 1);
      if (response && response.success) {
          callBack(response.data)
      } else {
          Message.error(response.msg);
      }
  },
 }
// 组件内
	this.props.dispatch({
		type:'preCenter/add',
		payload: params,
		callBack: result =>{
			console.log(result);
		}
	})

2、使用promise传递

// models
effects: {
    *addAfter1Second(action, { call, put }) {
      yield call(delay, 1000);
      const response = yield put({ type: 'add' });
      resolve(response)
    },
  },
// 组件内
new Promise((resolve) =>{
	this.props.dispatch({
		type: 'addAfter1Second',
		payload: params,
	})
}).then(result => {
	console.log(result)
})

3、使用dispatch的then方法

	// models
	reduers: {
		add(state, { payload }){}
	},
	effects: {
	    *addAfter1Second(action, { call, put }) {
	      yield call(delay, 1000);
	      yield put({ type: 'add' });
	    },
	  },
	// 组件内
	this.props.dispatch({
		type: 'addAfter1Second',
		payload: params,
	}).then(result => {
		console.log(result)
	})



版权声明:本文为qq_32615575原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。