Javascript 如何保存';这';回调函数中的实例

Javascript 如何保存';这';回调函数中的实例,javascript,angular,typescript,Javascript,Angular,Typescript,在Angular2中,我有一个组件,它使用一个服务将文件上传到AmazonS3 我的组件(简化): 我的服务(简化): private getS3():任意{ //获取或创建AWS实例 返回s3; } 公共上传文件(selectedFile,回调):布尔值{ 此文件为.getS3().upload({ Key:Key_name, ContentType:file.type, 正文:文件, StorageClass:“标准”, ACL:“私人” },函数(错误,数据){/使用箭头函数 },(err

在Angular2中,我有一个组件,它使用一个服务将文件上传到AmazonS3

我的组件(简化):

我的服务(简化):

private getS3():任意{
//获取或创建AWS实例
返回s3;
}
公共上传文件(selectedFile,回调):布尔值{
此文件为.getS3().upload({
Key:Key_name,
ContentType:file.type,
正文:文件,
StorageClass:“标准”,
ACL:“私人”

},函数(错误,数据){/使用箭头函数


},(err,data)=>{/您可以更改您的
上载文件
以返回一个

并按原样处理组件中的错误情况。 差不多

    public uploadFile(selectedFile): boolean {
        return new Promise((resolve, reject) => {
          this.getS3().upload({
            Key: key_name,
            ContentType: file.type,
            Body: file,
            StorageClass: 'STANDARD',
            ACL: 'private'
         }, function(err, data){ // <=== What to do here?!
            resolve(err, data)
        });
       }
  });

虽然@Gunter是正确的,但我认为您希望在实际传递给该函数的回调中保留
this

uploadCallback(err, data) {
  this._loading = false; // this is the "this" you want to keep
} 
那么它将是这样的:

this._s3service.uploadFile(fileObject, ()=>this._loading = false);
// or 
this._s3service.uploadFile(fileObject, ()=>this.uploadCallback());
// or
this._s3service.uploadFile(fileObject, this.uploadCallback.bind(this));
还请注意,使用
可观察的
而不是传递回调可能会很有趣:

public uploadFile(selectedFile): Observable<any> { // "<any>" because I don't know what is the type of "data"
    return Observable.create((observer) => {
        this.getS3().upload({
            Key: key_name,
            ContentType: file.type,
            Body: file,
            StorageClass: 'STANDARD',
            ACL: 'private'
        }, (err, data)=> {
            if(err)
              observer.error(err);
            else
              observer.next(data);
            observer.complete();
        });
    });
}

或者,如果出于某种原因不能或不想使用箭头函数,可以“绑定”函数(myFunction(){}.bind(this))。这是ES6之前的做法。我考虑过添加它,但对于问题中的示例,我认为这不是一个好主意。我对示例进行了修改,使其更有意义。我可能问得不够清楚:问题是回调的“this”引用的是服务,而不是组件。我需要callba中的代码“this”指向组件(而不是服务)时执行。如果我没有搞错,您的答案将显示如何将“this”绑定到服务。第一段代码的标题是:“My component(simplified)”。如果我不够清楚,我很抱歉。谢谢!再次强调,您可以将函数“绑定”到您想要的任何“this”(不必是“this”),它可以是另一个组件,等等)。这样,“绑定”可以比箭头函数更灵活。(有趣的是…你可以在任何地方“指向”一个“绑定”,但你只能在一个地方“指向”一个“箭头”…在某个地方所有东西的秘密…)为什么
解析(错误,数据)
,为什么不
解析(数据)
拒绝(错误)
?@n00dl3 yes error应该被拒绝。但是在这种情况下,它将取决于上传函数call的响应参数,问题是什么?
(err,data)=>err?reject(err):解析(data)
加上传递
回调在这里没有意义。@n00dl3 yes这应该可以工作(如果我们知道err对于成功上传来说是错误的)可能的解决方案也可能是使用实例函数。您理解我的问题是正确的。+1用于建议使用可观测值。
uploadCallback(err, data) {
  this._loading = false; // this is the "this" you want to keep
} 
this._s3service.uploadFile(fileObject, ()=>this._loading = false);
// or 
this._s3service.uploadFile(fileObject, ()=>this.uploadCallback());
// or
this._s3service.uploadFile(fileObject, this.uploadCallback.bind(this));
public uploadFile(selectedFile): Observable<any> { // "<any>" because I don't know what is the type of "data"
    return Observable.create((observer) => {
        this.getS3().upload({
            Key: key_name,
            ContentType: file.type,
            Body: file,
            StorageClass: 'STANDARD',
            ACL: 'private'
        }, (err, data)=> {
            if(err)
              observer.error(err);
            else
              observer.next(data);
            observer.complete();
        });
    });
}
this._s3service.uploadFile(fileObject).subscribe(data=>console.log(data))