這篇文章主要為大家展示了“微信小程序中前端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封裝的示例分析-創新互聯
標題URL:http://vcdvsql.cn/article18/cscegp.html
成都網站建設公司_創新互聯,為您提供軟件開發、搜索引擎優化、做網站、品牌網站設計、服務器托管、營銷型網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯