明天在線辦公沒時間學習了,今天晚上再更新一篇html
代碼提交一次:https://github.com/zhaogaojian/jgdemoios
一、src下建立services目錄git
建立文件userSrv.tsgithub
export async function query(): Promise<any> { return request('user/getuserinfo'); } import axios from "axios" export default async function request (options) { let response try { response = await axios(options) return response } catch (err) { return response } }
注意若是未安裝axios,請安裝axiosaxios
yarn add axios
三、User.tsx增長一個按鈕async
<Button type="primary" style={{marginTop:10,width:300}} onClick={this.handleReadFromSrvClick}>從服務端讀取</Button>
四、事件,不須要傳參數的話,payload能夠不填內容學習
handleReadFromSrvClick = e => { this.props.dispatch({ type:"User/getUserInfo", payload:null }) console.log('click: ', e); };
五、models目錄下的 user.ts中增長以下代碼this
import { query as queryUser } from '@/services/userSrv';
注意:下面effects不須要再使用queryUser.queryspa
effects: { *getUserInfo(_, { call, put }) { const response = yield call(queryUser); //yield console.log(response.data); yield put({ type: 'User/updateUserState', payload: response.data, }); } }
六、mock 增長user.ts3d
export default { '/user/getuserinfo': {UserInfo:{ username:"李四" }} };
七、運行效果
點擊從服務端讀取,姓名變成了李四,達到預期效果
八、不分層與後臺數據交互方法請參考
https://www.cnblogs.com/zhaogaojian/p/12238299.html