我正在使用redux-saga
来获取服务器api数据。我的问题是我正在尝试设计以下代码。
但是被注释掉的yield put(get01Action(members));
具有以下语法错误。
A 'yield' expression is only allowed in a generator body.
我不知道如何管理。
import '@babel/polyfill';
import { fork, take, put } from 'redux-saga/effects';
import axios from "axios";
export function* rootSaga(){
yield fork(fetch01);
yield fork(fetch02);
}
function* fetch01() {
while (true){
yield take('FETCH01_REQUEST');
axios.get('/api/members')
.then(function (response) {
// handle success
let members = response.data.data;
// yield put(get01Action(members));
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
}
}
function* fetch02() {
....
}
function get01Action(members){
return {
type: 'GET_MEMBERS',
member_id: 0,
members: members
}
}
请给我一些建议。
谢谢。
您可以使用通话效果进行axios通话,然后可以使用put。
现在它不起作用,因为您在promise call内使用yield。
function* fetch01() {
while (true){
try {
yield take('FETCH01_REQUEST');
const response = yeild call(axios.get, '/api/members');
yield put(get01Action(response.data.data))
} catch(err) {
console.error(err)
}
}
}