15、axios的使用與數(shù)據(jù)的mock①,15axios數(shù)據(jù)mock

具體的代碼請(qǐng)移步github
GitHub:https://github.com/Ewall1106/mall(請(qǐng)選擇分支15)

一、axios官方文檔基本閱讀

我們先從官方實(shí)例上上看看axios的用法:https://github.com/axios/axios

// Make a request for a user with a given ID
axios.get('/user?ID=12345')
    .then(function (response) {
    console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });

// Optionally the request above could also be done as
axios.get('/user', {
        params: {
            ID: 12345
        }
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });

// Want to use async/await? Add the `async` keyword to your outer function/method.async function getUser() {
    try {
        const response = await axios.get('/user?ID=12345');
        console.log(response);
    } catch (error) {
        console.error(error);
    }
}

上面的記個(gè)大概就好,我們動(dòng)手實(shí)踐一波。愛掏網(wǎng) - it200.com

二、新建mock.json

1、我們先在static文件夾下新建一個(gè)mock文件,里面放上我們首頁所需要的數(shù)據(jù)
(1)先是輪播圖的數(shù)據(jù),我們把首頁中的輪播圖鏈接復(fù)制過來:

img_85433b077ce00e2eaeaa2daf32859bdb.png mock數(shù)據(jù)

(2)然后是分類的icon圖片和推薦模塊相關(guān)數(shù)據(jù)

img_fd1624c49776eff74d6be6fbe9f731e7.png mock數(shù)據(jù)

三、axios的安裝和數(shù)據(jù)mock的一些配置

1、然后我們動(dòng)手先安裝一波axiosexpress,為什么要用到express,因?yàn)槲覀償?shù)據(jù)的mock中需要用到express框架實(shí)現(xiàn),后面我們?cè)谠敿?xì)講解expres。愛掏網(wǎng) - it200.com

(1)安裝express、axios

$ npm install express  --save
$ npm install axios  --save
img_610d112bfa743b67c2f30f3d0666b045.png install express and axios

(2)在webpack.dev.conf.js的頭部中引入

// mock數(shù)據(jù)
const express = require('express')
const app = express()
var appData = require('../static/mock/mock.json')
var router = express.Router()
// 通過路由請(qǐng)求本地?cái)?shù)據(jù)
app.use('/api', router)
img_c87fe374c276331d549cc1d1cded2216.png config配置

(3)devServer中添加before方法

// 添加before方法
before(app) {
    app.get('/api/appData', (req, res) => {
        res.json({
            errno: 0,
            data: appData
        })
    })
}
img_bc27f615b54d11238418e553c4268a00.png before()

四、使用axios獲取mock數(shù)據(jù)

1、我們進(jìn)入hom.vue頁面先引入axios;

2、然后我們?cè)?code>methods中寫個(gè)函數(shù):用axios獲取首頁數(shù)據(jù)并打印,然后當(dāng)vue生命周期為mounted階段時(shí)調(diào)用它:

img_187bbd4bfe0a3399841066c4ee39852a.png 使用axios獲取數(shù)據(jù)

3、最后我們進(jìn)入瀏覽器中看看數(shù)據(jù)是不是打印出來了

img_c51c5576dbfcbbd6180e9e3d91440f00.png 瀏覽器console

ok,我們mock的數(shù)據(jù)都拿到了。愛掏網(wǎng) - it200.com到了這一步,接下來就簡單了,無非是把值傳給組件,然后將數(shù)據(jù)渲染到頁面上,這個(gè)我們下篇講。愛掏網(wǎng) - it200.com

參考學(xué)習(xí)
https://github.com/axios/axios
https://github.com/Ewall1106/mall
https://www.jianshu.com/p/986821d35988
https://www.jianshu.com/p/4f92c4461e3d
https://www.jianshu.com/p/004b73f3f589

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評(píng)論
更多 網(wǎng)友評(píng)論0 條評(píng)論)
暫無評(píng)論

返回頂部

主站蜘蛛池模板: 亚洲国产午夜精品理论片| 色婷婷综合激情视频免费看| www卡一卡二卡三| 久久国产经典视频| 亚洲女人影院想要爱| 免费a级毛片出奶水| 国产亚洲欧美视频| 国产欧美日韩不卡| 国产精品自在线| 大桥未久恸哭の女教师| 成年女人免费视频| 日韩一品在线播放视频一品免费| 欧美亚洲综合在线| 欧美日韩电影在线| 永久免费观看的毛片的网站| 精品久久一区二区三区| 精品无码成人久久久久久| 边吃奶边摸下我好爽免费视频 | 欧美三级一级片| 欧美日韩一区二区三区自拍| 激情五月亚洲色图| 炕上摸着老妇雪白肥臀| 直接在线观看的三级网址| 秋霞免费一级毛片| 精品日韩欧美一区二区在线播放| 翁想房中春意浓1-28| 美女解开胸罩摸自己胸直播| 色一情一乱一伦一区二区三区日本 | 亚洲国产欧美91| 亚洲成av人片在线观看| 亚洲五月丁香综合视频| 亚洲中文字幕无码久久2020| 亚洲a∨无码男人的天堂| 亚洲Av鲁丝一区二区三区| 乱子伦xxxx| 久久精品国产99精品国产亚洲性色 | 香蕉久久夜色精品升级完成| 超级乱淫视频aⅴ播放视频| 老司机午夜在线视频免费观| 精品视频一区二区三区在线观看 | 91chinese在线|