如何解决类型“Observable<void>”不能分配给类型“Observable<HttpEvent<any>>”类型“void”不能分配给类型“HttpEvent<any>”
我正在尝试为上传添加进度条并出现此错误。 并且 event.type 未定义。
请帮我找到解决方案。谢谢
我附上了我已经完成的代码。
HttpRequest 代码
uploadfile(file: any): Observable<HttpEvent<any>>{
return this.InfoService.getId().pipe(
concatMap((result) => {
return this.schemaService.getSchema().pipe(schema => {
const url = '/url';
return this.http.post(url,file,{headers: this.headers,reportProgress: true});
}).toPromise().then((resolved) => {
this.successMessage('Upload Success',MessageIcon," uploaded successfully");
})
}));
}
订阅方式:
uploadfile(){
const formData = new FormData();
formData.append('file',this.uploadForm.get('item').value);
this.uploadService.uploadfile(formData).subscribe((event : HttpEvent<any>) => {
console.log(event)
switch (event.type) {
case HttpEventType.UploadProgress:
this.progress = Math.round(event.loaded / event.total * 100);
console.log(`Uploaded! ${this.progress}%`);
break;
case HttpEventType.Response:
console.log('successfull!',event.body);
setTimeout(() => {
this.progress = 0;
},1500);
}
})
}
解决方法
-
我不确定您想用
$chart->setOptions([ 'responsive' => true,'legend' => ['display' => false],'title' => [ 'display' => true,'text' => 'Title of the graph','fontSize' => 16 ],'scales' => [ 'yAxes' => [ 'title' => [ 'display' => true,'text' => 'Title for y Axes',] ],],]);
函数的schema
参数做什么。pipe
将 RxJS 运算符作为参数。从使用情况来看,我假设您想在这里使用像pipe
这样的映射运算符。 -
函数的返回类型声明为
concatMap
。但是您正在使用Observable<HttpEvent<any>>
函数将 Observable 转换为 Promise。您可以改用toPromise()
运算符。
试试下面的方法
tap
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。