如何将 returns 可观察到的函数转换为箭头函数?

How to convert a function that returns an observable to an arrow function?

我有以下代码可以正常工作。我想要的是将 getSaveObs 函数转换为箭头函数,以便我可以在保存函数中使用它。

private save(property: IProperty): void {
  // I want the getSaveObs logic to be moved here instead of a separate function
  this.getSaveObs(property)
   .subscribe(savedProperty => {
    this.property = savedProperty;
    this.propertyStorageService.storeProperty(this.property);
    this.propertyDetailsForm.get('id').setValue(this.property.id);
    // if new property, move to step 2
    if (!savedProperty.id) {
      console.log('New property, moving to step 2');
      if (this.clientId) {
        this.router.navigateByUrl('/clients/' + this.clientId + '/properties/' + this.property.id + '/' + '2');
      } else {
        this.router.navigate(['/properties', this.property.id, '2']);
      }
    }
  }, error => (console.error('Error adding/updating a property: ', error)));
}


private getSaveObs(property: IProperty): Observable<IProperty> {
  if (property.id) {
    if (this.clientId) {
        return this.clientPropertiesService.updateProperty(this.accountService.userIdentity.userId,                
           this.clientId, property);
    } else {
        return this.profileService.updateProperty(this.accountService.userIdentity.userId, property);
    }
  } else {
    // New property observable
    if (this.clientId) {
        return this.clientPropertiesService.createProperty(this.accountService.userIdentity.userId, 
           this.clientId, property);
    } else {
      return this.profileService.createProperty(this.accountService.userIdentity.userId, property);
    }
  }
}

这里是一种叫做 returns Observable

的方法
createUserProperty(property: IProperty, userDocument: 
    AngularFirestoreCollection): EntityResponseType {
    delete property.id;
    property.modifiedTs = firebase.firestore.Timestamp.now();
    property.createdTs = firebase.firestore.Timestamp.now();

    return from(userDocument.add(property))
      .pipe(
         map(res => {
         return {...property, id: res.id};
      })
    );
 }

为什么您认为箭头函数可以用在 save(property: IProperty): void 而常规函数不能?你想做什么?


不管任何函数都可以变成这样的箭头函数

function someName(arg: ArgType): ReturnType {
  /*...function body...*/
}

变成

const someName = (arg: ArgType): ReturnType => {
  /*...function body...*/
}

所以在你的情况下,那是:

const getSaveObs = (property: IProperty): Observable<IProperty> => {
  if (property.id) {
    if (this.clientId) {
        return this.clientPropertiesService.updateProperty(this.accountService.userIdentity.userId,                
           this.clientId, property);
    } else {
        return this.profileService.updateProperty(this.accountService.userIdentity.userId, property);
    }
  } else {
    // New property observable
    if (this.clientId) {
        return this.clientPropertiesService.createProperty(this.accountService.userIdentity.userId, 
           this.clientId, property);
    } else {
      return this.profileService.createProperty(this.accountService.userIdentity.userId, property);
    }
  }
}