这个错误提示表明在类型为 void & Promise<UploadTask>
的对象上不存在 on
方法。
const task = await request.uploadFile(context, {
url: 'https://hmajax.itheima.net/api/uploadimg',
header: {
'content-type': 'multipart/form-data'
},
method: http.RequestMethod.POST,
data: [],
files: [{
filename: imgName,
name: 'img',
uri: `internal://cache/${imgName}`,
type: imgName?.split('.').pop()
}]
})
task.on('headerReceive', (res: object) => {
const IRes = res as IUploadRes
const imgUrl = JSON.parse(IRes.body) as IBody
//AlertDialog.show({ message: imgUrl.url })
this.avatar = imgUrl.data.url
})
task的类型
const task: void & Promise<request.UploadTask>
uploadFile的方法
按照他的写法
export interface AsyncCallback<T, E = void> {
(err: BusinessError<E>, data: T): void;
}
request.uploadFile(context, {
url: 'https://hmajax.itheima.net/api/uploadimg',
header: {
'content-type': 'multipart/form-data'
},
method: http.RequestMethod.POST,
data: [],
files: [{
filename: imgName,
name: 'img',
uri: `internal://cache/${imgName}`,
type: imgName?.split('.').pop()
}]
}, (err,task) => {
task.on('headerReceive', (res: object) => {
const IRes = res as IUploadRes
const imgUrl = JSON.parse(IRes.body) as IBody
//AlertDialog.show({ message: imgUrl.url })
this.avatar = imgUrl.data.url
})
})
可以看到这样就解决问题了