十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要講解了“微信小程序中前端promise怎么封裝”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“微信小程序中前端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 => { //狀態(tài)碼 toString() 轉(zhuǎn)成字符串 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: '網(wǎng)絡(luò)錯(cuò)誤', 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怎么封裝”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對微信小程序中前端promise怎么封裝這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!