国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序前端promise封裝代碼實例

2019-11-19 10:59:09
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了微信小程序前端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)  }) }})

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿鲁科尔沁旗| 长泰县| 海南省| 辽阳市| 连南| 丰顺县| 高台县| 青川县| 汾西县| 西林县| 陇南市| 太谷县| 常熟市| 吴川市| 通渭县| 彭山县| 垣曲县| 昭通市| 仁化县| 临桂县| 荣昌县| 永春县| 禄劝| 龙门县| 同心县| 定南县| 大港区| 合川市| 游戏| 安达市| 兴山县| 砚山县| 兴隆县| 苏尼特左旗| 岑巩县| 上思县| 桦川县| 玛纳斯县| 景东| 镇赉县| 黄冈市|