1. 在 utils 文件夹创建一个 config.js
export default {
host: "http://localhost:3000",
}
2. 在 utils 文件夹创建一个 request.js 进行请求封装
// 请求的二次封装
// 保留静态代码,抽离动态代码
import config from "./config";
export default (url, data = {}, method = "GET")=> {
return new Promise((resolve,reject) => {
wx.request({
url: config.host + url,
data,
method,
success: (res)=>{
// 请求成功
// this 是为undefined,所以不能使用this.setData设置数据了
console.log(res);
resolve(res.data)
},
fail: (err)=>{
// 请求失败
console.log(err);
reject(err)
},
});
}
}
2. 在文件中进行导入使用
import request from "../../utils/request.js"
onLoad: async function (options) {
const listData = await request("/banner", {type: 2})
this.setData({
list: listData.data
})
console.log(listData)
}