微信小程序前端promise封裝代碼實(shí)例
更新時(shí)間:2019年08月24日 11:28:55 作者:達(dá)叔小生
這篇文章主要介紹了微信小程序前端promise封裝代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
這篇文章主要介紹了微信小程序前端promise封裝代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
代碼如下
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)
})
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js+div實(shí)現(xiàn)圖片滾動(dòng)效果代碼
本篇文章主要是對js+div實(shí)現(xiàn)圖片滾動(dòng)效果的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
javascript中刪除指定數(shù)組中指定的元素的代碼
已知一個(gè)數(shù)組,我們想用指定的方法對數(shù)組中的元素進(jìn)行逐一操作。需要的朋友可以參考下。2011-02-02
javascript實(shí)現(xiàn)分欄顯示小技巧附圖
考試頁面可以實(shí)現(xiàn)隱藏左邊的考生信息部分,學(xué)了javascript后也能實(shí)現(xiàn)這個(gè)功能了,下面是實(shí)現(xiàn)思路、代碼及解效果截圖,喜歡的朋友們可以看看2014-10-10

