Vue框架中怎么调用模拟数据(vue,开发技术)

时间:2024-05-08 19:25:21 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

1、框架结构

Vue框架中怎么调用模拟数据

mock是模拟数据文件夹,文件夹中有index.js,里面包含所模拟的接口数据:如下所示

constMock=require("mockjs");const{param2Obj}=require("./utils");constuser=require("./user");//调用方式constmocks=[...user];functionmockXHR(){//mockpatch//https://github.com/nuysoft/Mock/issues/300Mock.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);};functionXHR2ExpressReqWrap(respond){returnfunction(options){letresult=null;if(respondinstanceofFunction){const{body,type,url}=options;//https://expressjs.com/en/4x/api.html#reqresult=respond({method:type,body:JSON.parse(body),query:param2Obj(url),});}else{result=respond;}returnMock.mock(result);};}for(constiofmocks){Mock.mock(newRegExp(i.url),i.type||"get",XHR2ExpressReqWrap(i.response));}}module.exports={mocks,mockXHR,};

2、在api中进行调用:如图

Vue框架中怎么调用模拟数据

然后就可以成功请求数据

Vue框架中怎么调用模拟数据

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Vue框架中怎么调用模拟数据的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Android中Apk反编译及加密的方法下一篇:

2 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18