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

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

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

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中進行調(diào)用:如圖

在這里插入圖片描述

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

在這里插入圖片描述

總結(jié)

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

相關(guān)文章

最新評論

慈溪市| 封开县| 昌邑市| 即墨市| 镇平县| 灵川县| 明溪县| 壤塘县| 德钦县| 类乌齐县| 张家口市| 盐边县| 吉木乃县| 大姚县| 南涧| 宝丰县| 尉氏县| 邯郸市| 深泽县| 黎城县| 武山县| 麟游县| 突泉县| 明溪县| 崇信县| 巴青县| 钟山县| 柞水县| 观塘区| 湄潭县| 拉萨市| 基隆市| 黄梅县| 萍乡市| 湘西| 沐川县| 修文县| 南通市| 凤庆县| 宿州市| 延安市|