小編給大家分享一下react如何mock數(shù)據(jù),希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

react mock數(shù)據(jù)的方法:首先在public目錄下建立【/api/headerList.json】文件;然后加入相關(guān)代碼【axios.get('/api/headerList.json')】。
react mock數(shù)據(jù)的方法:
如下代碼,在input獲得焦點(diǎn)時(shí)調(diào)用getList()方法。
const mapDispathToProps = (dispatch) => {
return {
handleInputFocus() {
dispatch(actionCreators.getList())
}
}
}getList()方法是通過Ajax獲取數(shù)據(jù),此時(shí)后端并沒有寫好,所以自己mock一個(gè)數(shù)據(jù)。
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')這句代碼,計(jì)算機(jī)會(huì)首先去src目錄下尋找/api/headerList.json,沒有找到然后去public目錄下找,此時(shí)我們在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","把","上","這","下","的","了","來"]
}看完了這篇文章,相信你對(duì)react如何mock數(shù)據(jù)有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站題目:react如何mock數(shù)據(jù)-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://chinadenli.net/article12/gecgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化、微信小程序、小程序開發(fā)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容