采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
import axios from 'axios'; import Config from 'react-native-config'; axios.defaults.baseURL = Config.API_URL; axios.interceptors.request.use( function (config) { return config; }, function (error) { return Promise.reject(error); }, ); axios.interceptors.response.use( function (response) { console.log(response); return response.data; }, function (error) { return Promise.reject(error); }, );
import {Model, Effect} from 'dva-core-ts'; import {Reducer} from 'redux'; import axios from 'axios'; const CAROUSEL_URL = '/mock/11/carousel'; export interface ICarousel { id: string; image: string; colors: [string, string]; } export interface HomeState { carousel: ICarousel[]; } interface HomeModel extends Model { namespace: 'home'; state: HomeState; reducers: { setState: Reducer<HomeState>; }; effects: { fetchCarousel: Effect; }; } const initalState = { carousel: [], }; const homeModel: HomeModel = { namespace: 'home', state: initalState, reducers: { setState(state = initalState, {payload}) { return { ...state, ...payload, }; }, }, effects: { *fetchCarousel(_, {call, put}) { const {data} = yield call(axios.get, CAROUSEL_URL); yield put({ type: 'setState', payload: { carousel: data, }, }); }, }, }; export default homeModel;
访问http://127.0.0.1:3001/mock/11/carousel 有数据 { status: 100, data: [ { id: "610000201704215320", image: "http://39.105.213.120/images/3.jpg", colors: [ "#f2e479", "#c179f2" ] }, { id: "820000201207071839", image: "http://39.105.213.120/images/3.jpg", colors: [ "#79f29e", "#f27a79" ] }, { id: "710000199206193775", image: "http://39.105.213.120/images/47.jpg", colors: [ "#799af2", "#bef279" ] } ], msg: "success" }
登录后可查看更多问答,登录/注册
从入门到实战,掌握用TypeScript开发ReactNative应用
了解课程