小程序封装axios
关于封装axios
·
const URL = "此处为接口的公共地址"
const api = (params) => {
return new Promise((resolve, reject) => {
uni.showLoading({
title: "加载中"
})
uni.request({
url: URL + params.url,
method: params.method || "GET",
data: params.data || "",
success: (res) => {
uni.hideLoading()
resolve(res.data)
},
fail: (err) => {
uni.hideLoading()
},
complete: () => {
uni.hideLoading()
}
})
})
}
export default api
import api from "../request.js"
export const getBanner = async ()=>{
let data = await api({
url:'此处为接口详细地址'
})
return data
}
import {getBanner} from '../../request/home/home.js'
export default {
onLoad: () => {
getBanner().then((res)=>{
console.log(res)
})
}
}
文件位置如图所示
更多推荐
已为社区贡献2条内容
所有评论(0)