1.页面根目录下新建api 文件夹,作用发起请求
2. api文件夹下建一个http.js 用来发送promise请求
import baseUrl from "./baseUrl"
var fun = function (config) {
return new Promise((resolve, reject) => {
wx.request({
url: baseUrl + config.url,
method:config.method,
success: res => {
resolve(res)
console.log('res数据成功了', res);
}
})
})
}
export default fun
http.js 同级新建js文件 baseUrl ,用来设置共同的请求头
export default "https://autumnfish.cn"
api 下新建 index文件夹 文件夹下 设置 index.js文件
import http from "../http"
function getList(data) {
return http({
url: '/top/artists',
data ,
method: "GET",
})
}
export {getList}
在需要用到的地方
导入
import {
getList
}
from "../../api/index/index"
onLoad(options) {
getList({里面放请求的参数}).then(res => console.log(res.data.artists))
},