您好,登錄后才能下訂單哦!
小編給大家分享一下react如何mock數據,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
react mock數據的方法:首先在public目錄下建立【/api/headerList.json】文件;然后加入相關代碼【axios.get('/api/headerList.json')】。
react mock數據的方法:
如下代碼,在input獲得焦點時調用getList()
方法。
const mapDispathToProps = (dispatch) => { return { handleInputFocus() { dispatch(actionCreators.getList()) } } }
getList()方法是通過Ajax獲取數據,此時后端并沒有寫好,所以自己mock一個數據。
export const getList = () => { return (dispatch) => { axios.get('/api/headerList.json').then((res) => { const data = res.data; console.log(data) }).catch(() => { console.log('error') }) } }
axios.get('/api/headerList.json')
這句代碼,計算機會首先去src目錄下尋找/api/headerList.json,沒有找到然后去public目錄下找,此時我們在public目錄下建立/api/headerList.json文件,文件如下:
{ "success":true, "data":["微信","支付寶","螞蟻金服","被騙","借唄","詐騙","盜竊","pandas","TensorFlow","PyTorch","Caffe","scikit-learn","Python","Keras","pyecharts","ggplot","Matplotlib","Gensim","Bokeh","Theano","Scrapy","SciPy","Plotly","NumPy","XGBoost","是","他","有","光大永明","我","要","人","Android","女","大","不","著","男","它","二","于","中","java","把","上","這","下","的","了","來"] }
看完了這篇文章,相信你對react如何mock數據有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。