最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue框架中如何調(diào)用模擬數(shù)據(jù)你知道嗎

 更新時(shí)間:2022年03月31日 12:53:25   作者:時(shí)間的情敵  
這篇文章主要為大家詳細(xì)介紹了Vue框架中如何調(diào)用模擬數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

1、框架結(jié)構(gòu)

在這里插入圖片描述

mock是模擬數(shù)據(jù)文件夾,文件夾中有index.js,里面包含所模擬的接口數(shù)據(jù):如下所示

const Mock = require("mockjs");
const { param2Obj } = require("./utils");

const user = require("./user");

//調(diào)用方式
const mocks = [...user];
function mockXHR() {
    // mock patch
    // https://github.com/nuysoft/Mock/issues/300
    Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send;
    Mock.XHR.prototype.send = function() {
        if (this.custom.xhr) {
            this.custom.xhr.withCredentials = this.withCredentials || false;

            if (this.responseType) {
                this.custom.xhr.responseType = this.responseType;
            }
        }
        this.proxy_send(...arguments);
    };

    function XHR2ExpressReqWrap(respond) {
        return function(options) {
            let result = null;
            if (respond instanceof Function) {
                const { body, type, url } = options;
                // https://expressjs.com/en/4x/api.html#req
                result = respond({
                    method: type,
                    body: JSON.parse(body),
                    query: param2Obj(url),
                });
            } else {
                result = respond;
            }
            return Mock.mock(result);
        };
    }

    for (const i of mocks) {
        Mock.mock(
            new RegExp(i.url),
            i.type || "get",
            XHR2ExpressReqWrap(i.response)
        );
    }
}

module.exports = {
    mocks,
    mockXHR,
};

2、在api中進(jìn)行調(diào)用:如圖

在這里插入圖片描述

然后就可以成功請(qǐng)求數(shù)據(jù)

在這里插入圖片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容! 

相關(guān)文章

最新評(píng)論

浑源县| 平江县| 苍梧县| 天峨县| 库伦旗| 北碚区| 汉中市| 福安市| 泰宁县| 师宗县| 德江县| 神木县| 伊宁县| 江油市| 靖安县| 根河市| 永昌县| 四会市| 连云港市| 渝中区| 洛扎县| 邵武市| 文成县| 三都| 维西| 来凤县| 宝清县| 康定县| 根河市| 鲁山县| 陇西县| 临江市| 昌都县| 朔州市| 郎溪县| 耿马| 南宫市| 茶陵县| 留坝县| 甘谷县| 巴里|