微信小程序中前端promise怎么封装
这篇文章主要讲解了“微信小程序中前端promise怎么封装”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“微信小程序中前端promise怎么封装”吧!

创新互联服务项目包括房山网站建设、房山网站制作、房山网页制作以及房山网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,房山网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到房山省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
config.js
const config = {
base_url_api : "https://douban.uieee.com/v2/movie/",
}
export {config}http.js
import { config } from "../config";
class HTTP {
requset({ url, method = "GET", data = {} }) {
const promise = new Promise((resolve, reject) => {
wx.request({
url: config.base_url_api + url,
data,
method,
header: {
'Content-Type': 'json'
},
success: res => {
//状态码 toString() 转成字符串
const statusCode = res.statusCode.toString();
if (statusCode.startsWith("2")) {
resolve(res.data)
} else {
this._show_error();
}
},
fail: res => {
reject(err);
this._show_error();
}
})
})
return promise;
}
_show_error() {
wx.showToast({
title: '网络错误',
icon: 'none'
})
}
}
export { HTTP }model/movie.js
import {HTTP} from "../utils/http";
class MovieModel extends HTTP{
getInTheaters(){
return this.requset({
url:"in_theaters"
})
}
getTop250(){
return this.requset({
url:"top250"
})
}
getComingSoon(){
return this.requset({
url:"coming_soon"
})
}
}
export {MovieModel};pages/index/index.js
const app = getApp();
import {MovieModel} from "../../model/movie";
const movieModel = new MovieModel();
Page({
onLoad(){
// movieModel.getInTheaters().then(res=>{
// console.log(res)
// })
const inTheaters = movieModel.getInTheaters()
const top250 = movieModel.getTop250();
const comingSoon = movieModel.getComingSoon();
Promise.all([inTheaters,top250,comingSoon]).then(res=>{
let[inTheaters,top250,comingSoon] = res;
console.log(inTheaters)
})
}
})感谢各位的阅读,以上就是“微信小程序中前端promise怎么封装”的内容了,经过本文的学习后,相信大家对微信小程序中前端promise怎么封装这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
本文题目:微信小程序中前端promise怎么封装
链接地址:http://www.jxjierui.cn/article/jigogs.html


咨询
建站咨询
