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)文章
Ant Design moment對象和字符串之間的相互轉(zhuǎn)化教程
這篇文章主要介紹了Ant Design moment對象和字符串之間的相互轉(zhuǎn)化教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue3中實現(xiàn)使用element-plus調(diào)用message
這篇文章主要介紹了vue3中實現(xiàn)使用element-plus調(diào)用message,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Vue 級聯(lián)下拉框的設(shè)計與實現(xiàn)
在前端開發(fā)中,級聯(lián)選擇框是經(jīng)常用到的,這樣不僅可以增加用戶輸入的友好性,還能減少前后端交互的數(shù)據(jù)量。本文就介紹一下使用Vue實現(xiàn)級聯(lián)下拉框,感興趣的可以了解一下2021-07-07