## :-: Promise回调,类式封装
### 可修改式基础地址
```
//config.js
var config={
base_api_url:"http://douban.uieee.com/v2/"
}
export {
config
}
```
### Promise封装HTTP
```
//utils/http.js
import { config } from "../config";
class HTTP {
request({url,method="GET",data={}}) {
const promise = new Promise((resolve, reject) => {
wx.request({
url: config.base_api_url + url,
data,
method,
header: {
'Content-Type': 'json',
},
success: (res=>{
const statusCode = res.statusCode.toString();
if (statusCode.startsWith("2")) {
resolve(res.data);
} else {
this._show_error();
}
}),
fail: (err=> {
reject(err);
this._show_error();
})
})
})
return promise;
}
_show_error() {
wx.showToast({
title: '网络错误',
icon: 'none'
})
}
}
export {
HTTP
};
```
### 继承类MovieModel再次封装,使用返回值,返回到promise
```
//models/movie.js
import {HTTP} from "../utils/http";
class MovieModel extends HTTP{
getInTheaters(){
return this.request({
url:"in_theaters"
})
}
getTop250(){
return this.request({
url:"top250"
})
}
getComingSoon(){
return this.request({
url:"coming_soon"
})
}
}
export {MovieModel};
```
### 页面调用HTTP,通过then关键字调用promise
`Promise.all().then(res=>{})`,`res`会生成一个数组,需要用数组装载
```
import {MovieModel} from "../../models/movie-p";
const movieModel = new MovieModel();
Page({
onLoad(){
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);
console.log(top250);
console.log(comingSoon);
})
}
})
```
- 接口文档
- 1.开发环境的配置
- 2.wxml语法
- 2.1数据绑定
- 2.2列表渲染
- 2.3条件渲染
- 2.4模板引用
- 2.5事件
- 冒泡与非冒泡
- 2.6skill
- 2.7小程序接口调用
- 2.8获取自定义的属性值
- 2.9 Class动态绑定
- 3.小程序API组件
- 3.1音乐播放
- demo
- 3.2target与currentTarget
- 3.3下方导航页切换(tobar)
- 3.4let,var和const的区别
- 3.5轮播
- 3.6map
- 3.7 previewImage
- 3.8 chooseImage使用照相机
- 3.9 onReachBottom上拉刷新
- 3.10 下拉加载
- 3.11 使用缓存
- 3.12 scroll-view
- 3.13 webview
- 3.14 导航条加载,页面加载
- 3.15 模态框,弹出框
- 3.16 修改顶部标题
- 3.17 form,input获取内容
- 4.封装模板与自定义组件
- 4.1 父组件向子组件传参
- 4.2 behavior将子组件公共的部分封装
- 4.3 子组件自定义事件向组件传参
- 4.4 slot--父组件向子组件传递wxml
- 4.5 外部样式--父组件向子组件传递wxss
- 4.6 组件向页面传参
- 5.生命周期
- 6.HTTP封装
- 2.初代
- 4.终级豪华进化版本
- 7.精选小语法
- 7.1 text标签
- 7.2 wxs文件
- 7.3路径
- 7.4 小程序正则
- 8.demo
- 1.取数据并渲染的小程序
- data
- local.js
- images
- pages
- template
- template.wxml
- template.wxss
- index
- index-detail
- index-detail.js
- index-detail.json
- index-detail.wxml
- index-detail.wxss
- index.js
- index.json
- index.wxml
- index.wxss
- map
- map.js
- map.json
- map.wxml
- map.wxss
- movies
- movies.js
- movies.json
- movies.wxml
- movies.wxss
- web-page
- welcome
- utils
- utils.js
- app.js
- app.json
- app.wxss