[英]How to convert a function that returns an observable to an arrow function?
I have the following code which works fine.我有以下代码可以正常工作。 What I want is to convert the getSaveObs function to an arrow function so I can use it inside the save function.
我想要的是将 getSaveObs function 转换为箭头 function,这样我就可以在存档 function 中使用它。
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);
}
}
}
Here is one of the methods called that returns Observable这是返回 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};
})
);
}
Why do you believe that an arrow function can be used in save(property: IProperty): void
and a regular function cannot?为什么您认为箭头 function 可以用于
save(property: IProperty): void
而普通的 function 不能? What are you trying to do?你想做什么?
Regardless any function can be turned into an arrow function like this不管任何 function 都可以像这样变成箭头 function
function someName(arg: ArgType): ReturnType {
/*...function body...*/
}
becomes成为
const someName = (arg: ArgType): ReturnType => {
/*...function body...*/
}
SO in your case, that's:所以在你的情况下,那是:
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.