/** get 请求 * @param {接口地址} url * @param {请求参数} params */ get(url,params){ return new Promise((resolve,reject) => { wx.request({ url:url, data:params, success(res){ resolve(res.data); }, fail(err){ reject(err); } }) }) } /** post 请求 * @param {接口地址} url * @param {请求参数} params */ post(url,params){ return new Promise((resolve,reject) => { wx.request({ url:url, type:'POST', data:params, success(res){ resolve(res.data); }, fail(err){ reject(err); } }) }) }
上面是代码封装
//调用 async getData(){ let url = this.testUrl+'main/scpz/searchGs.php'; this.isShow = ''; let param = { gsmc:this.gsmc, action:'query' } let res = await this.get(url,param); let data = res.data; if(data){ console.log(data) this.searchData=data; this.hasData = '1'; this.$apply(); }else{ this.hasData = '2'; this.$apply(); } }
如果多重的话
//先封装一下function delay(word){ return new Promise((reslove,reject)=>{ setTimeout(()=>{ reslove('hello' +word) },2000) }) }// async+await一起使用async function start(){ const word1 = await delay('1111') console.log(word1) const word2 = await delay('2222') console.log(word2) const word3 = await delay('3333') console.log(word3)}start()