在redux-saga中,可以通过call()函数来发起异步请求。如果需要传递urls、params和header到call()函数中,可以通过以下步骤实现:
import { call } from 'redux-saga/effects';
function* fetchData(action) {
const { url, params, headers } = action.payload;
try {
const response = yield call(fetch, url, { params, headers });
// 处理响应数据
} catch (error) {
// 处理错误
}
}
export const fetchUserData = (url, params, headers) => ({
type: 'FETCH_USER_DATA',
payload: { url, params, headers },
});
import { takeEvery } from 'redux-saga/effects';
function* watchFetchData() {
yield takeEvery('FETCH_USER_DATA', fetchData);
}
import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga';
import rootReducer from './reducers';
import { watchFetchData } from './sagas';
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
rootReducer,
applyMiddleware(sagaMiddleware)
);
sagaMiddleware.run(watchFetchData);
export default store;
通过以上步骤,你可以在redux-saga中成功传递urls、params和header到call()函数中,实现异步请求的处理。请注意,以上代码仅为示例,实际应用中可能需要根据具体情况进行调整。
领取专属 10元无门槛券
手把手带您无忧上云