当前位置: 首页 > Web前端 > HTML

redux-saga学习(三)

时间:2023-04-02 17:25:01 HTML

redux-sagainstallredux-saga(参考github地址:链接说明)yarnaddredux-sagaredux-sagasimplyuseimport{createStore,applyMiddleware,compose}from'redux';importcreateSagaMiddlewarefrom'redux-saga';importmySaga来自'./sagas';//从'redux-thunk'导入thunk;从'./reducer'导入减速器;constsagaMiddleware=createSagaMiddleware()constcomposeEnhancers=window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?窗口.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?窗口.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?=composeEnhancers(applyMiddleware(sagaMiddleware));conststore=createStore(reducer,enhancer);sagaMiddleware.run(mySaga);导出默认存储;相应的创建sagas.js,把相应的request放在sagas.js中去管理actionCreators';从'axios'导入axios;function*fetchUser(action){try{letresData=yieldaxios.get('/list.json');常量数据=resData.数据列表;控制台日志(数据);yieldput(initList(data))}catch(e){console.log("Networkrequestfailed")}}function*mySaga(){yieldtakeEvery(GET_LIST_DATA,fetchUser);}导出默认mySaga;