【发布时间】:2020-06-08 14:21:53
【问题描述】:
我一直在尝试将ng2-file-upload 包中的onCompleteItem 数组函数绑定到我可以订阅的RxJS Observable 方法。
这是我正在谈论的功能:
onCompleteItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any;
..which 在使用中几乎相同为:
onCompleteItem(item: any, response: any, status: any, headers: any)
以下是 MyComponent 中的相关部分,目前正在运行:
imageArray: Image[] = [];
uploader:FileUploader = new FileUploader({
url: '/api/FileUpload'
});
ngOnInit() {
this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
const data = JSON.parse(response);
data.forEach(x => {
console.log(x);
this.imageArray.push(x);
}
this.uploader.removeFromQueue(item);
};
}
我尝试将其绑定到 Observable 中是通过使用 RxJS bindCallback 函数:
bindCallback(this.uploader.onCompleteItem, {item: any, response: any, status: any, headers: any})().pipe(
tap((item, response) => {
const data = JSON.parse(response);
this.uploader.removeFromQueue(item);
const images = data.map(x => console.log(x) );
this.imageArray.push(...images);
})
).subscribe();
这不起作用,因为有一些类型/语法错误。有人对如何将此重写为绑定的 Observable 函数有任何好的建议吗?
【问题讨论】:
标签: angular typescript observable ng2-file-upload