亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Redux-saga 如何在 saga 內部向請求傳遞參數

Redux-saga 如何在 saga 內部向請求傳遞參數

嚕嚕噠 2022-05-26 16:46:30
我有一些代碼要更新。這不是我的代碼,所以我不想要太多更改。它使用 redux-saga 和 axios。我想將一些論點傳遞給 saga我在sagas.js中的傳奇export function* getInfoSaga() {    while (true) {        yield take(mutations.GET_INFO);        const url1 = `${url}/api/getInfo/1-100`;        const logInfo = yield axios.get<any>(url1).then(response => response);        yield put(mutations.setInfo(logInfo.data.data));    }}突變.jsexport const GET_INFO = 'GET_INFO';export const getInfo = () => ({    type: GET_INFO,});sagas 在index.js中運行const s = sagas as any;for (const saga in s) {    sagaMiddleware.run(s[saga]);}在某些文件中,該操作由帶有事件的按鈕運行: onClick={() => dispatch({                        type: 'GET_INFO',                    })}我想傳遞一些參數來修改請求 url,我嘗試在 dipatch 請求中獲取附加對象,并將參數添加到 sagas.js 和 mutatuions.js 中的生成器,但是我得到一個錯誤,無法訪問未定義的屬性
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

你有沒有嘗試過:


onClick={() => dispatch({

  type: 'GET_INFO',

  url: 'your/url/goes/here'

})}


export function* getInfoSaga(action) {

    while (true) {

        yield take(mutations.GET_INFO);

        const url1 = `${action.url}/api/getInfo/1-100`;

        const logInfo = yield axios.get<any>(url1).then(response => response);

        yield put(mutations.setInfo(logInfo.data.data));

    }

}


查看完整回答
反對 回復 2022-05-26
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

更新:傳遞有效載荷并僅從“take”分配返回值


onClick={() => dispatch({

  type: 'GET_INFO',

  payload: {

    key1: value1

    // other values here

  }

})}


export function* getInfoSaga() {

   const action = yield take(mutations.GET_INFO);

   // action.payload.key1

}


查看完整回答
反對 回復 2022-05-26
  • 2 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號