在项目中我们使用请求 需要通过api封装
- 为什么封装api
在项目中 请求使用的地方会有很多 不利于后期维护 如果我们将api封装出来 只需要维护api文件就可以了
这里以 微信小程序 封装api来写小程序为例
第一步:(直接复制即可)
// 封装请求
const http = ({ url, method = '', data = '' }) => {
return new Promise((resolve, reject) => {
wx.request({
method: method,
// 公共地址前缀
url: `???` + url,
data: data,
// 请求头 定义要请求的类型 (不需要的可以不填)
header: {
'content-type': 'application/x-www-form-urlencoded'