以下为封装的request请求泛型函数,函数为入参url及options(可为空)
HBuilder X: 4.12 (alpha)
系统: MacOS 14.2
运行环境: 安卓模拟器13
type Options = {
header ?: UTSJSONObject
data ?: any
timeout ?: number
method ?: RequestMethod
}
function http<T>(uri : string, op ?: Options) : Promise<T> {
const url = `${getApp().globalData.serviceUrl}${uri}`
const header = {
Authorization: `Bearer ${state.token}`
} as UTSJSONObject
return new Promise<T>((resolve, reject) => {
uni.request({
url,
data: op?.data,
header: header ?? null,
method: op?.method ?? 'GET',
success: res => {
resolve(res.data as T)
},
fail: (err) => {
reject(err)
},
complete: () => {
}
})
})
}
export default http
函数调用/编译运行日志报错在附件,各种类型推断错误/及参数未传