angular - 定义可观察的接口
问题描述
我在 angular component.ts 文件中有一个函数,例如
createOrUpdateAlertingProfile() {
**let request: Observable<any>;**
let successMessage: string;
if (!this.alertingProfile) {
const slackConfigurationIdvalue = this.createOrEditAlertingProfileForm.get('slackConfigurationId').value;
const organizationIdvalue = this.createOrEditAlertingProfileForm.get('organizationId').value;
const remiderValue = this.createOrEditAlertingProfileForm.get('reminder').value;
const newAlertingProfileValue: AlertingProfileCreation = {
...this.createOrEditAlertingProfileForm.value,
slackConfigurationId: slackConfigurationIdvalue === '' ? undefined : slackConfigurationIdvalue,
organizationId: organizationIdvalue === '' ? undefined : organizationIdvalue,
reminder: remiderValue === '' ? undefined : remiderValue,
emails: this.emails.length === 0 ? undefined : this.emails.value,
webhooks: this.webhooks.length === 0 ? undefined : this.webhooks.value,
};
request = this.alertingProfilesService.createAlertingProfile(newAlertingProfileValue);
successMessage = 'New alerting profile was successfully added';
}
if (this.alertingProfile) {
request = this.alertingProfilesService.updateAlertingProfile(this.createOrEditAlertingProfileForm.value);
successMessage = `Alerting profile ${this.alertingProfile.name} was updated`;
}
request.subscribe(
() => {
this.alertify.success(successMessage);
this.isLoading$.next(false);
this.dialogRef.close(true);
},
(error) => {
this.alertify.error('\n' + error);
this.isLoading$.next(false);
},
);
}
“请求”的类型是可观察的
请求调用两个服务之一
createAlertingProfile(newAlertingProfile: AlertingProfileCreation) {
return this.http.post(`${this.baseUrl}/create`, newAlertingProfile);
}
updateAlertingProfile(model: AlertingProfileUpdate) {
return this.http.post(`${this.baseUrl}/edit`, model);
}
这两个接口使用了“AlertingProfileCreation”和“AlertingProfileUpdate”
export interface AlertingProfileUpdate {
name: string;
slackConfigurationId: number;
organizationId: number;
reminder: number;}
export interface AlertingProfileCreation {
name: string;
slackConfigurationId: number;
organizationId: number;
emails: AlertingProfileEmail[];
webhooks: Webhook[];
reminder: number;}
我的问题是我想为可观察的请求定义类型,当我尝试时
let request: Observable<AlertingProfileUpdate | AlertingProfileCreation>
它抛出错误,我如何定义 Observable 的接口?
解决方案
You can use Observable<AlertingProfileUpdate> | Observable<AlertingProfileCreation>
but keep in mind that you will be able to access only the crossesction of both interfaces (in typesafe manners speaking)
Also, keep in mind that you should cast your http calls as well.
createAlertingProfile(newAlertingProfile: AlertingProfileCreation):Observable<ReturnTypeOfCreation> {
return this.http.post<ReturnTypeOfCreation>(`${this.baseUrl}/create`, newAlertingProfile);
}
updateAlertingProfile(model: AlertingProfileUpdate):Observable<ReturnTypeOfUpdate> {
return this.http.post<ReturnTypeOfUpdate>(`${this.baseUrl}/edit`, model);
}
推荐阅读
- php - 在 PHP 中使用和执行 CREATE TEMPORARY TABLE 时出错
- postgresql - 使用 Row_number 更新大于 1 的行以匹配第 1 行中的值
- sass - 在 scss 文件中清除 IDE 分析警告“无法找到变量”以获取 mixin @content 指令中使用的变量?
- .htaccess - Search Console 无法重定向页面(htaccess 问题?)
- tags - 如何通过 vsphere rest api 配置 vm 标签
- javascript - 渲染错误:“TypeError:无法读取未定义的属性 '0'”v-for 循环
- azure - 在 Azure Maps 中显示矢量切片 (PBF)
- oop - 为什么 Haskell 不需要工厂模式?以及在 Haskell 中,OOP 中的模式解决的需求是如何解决的?
- php - SSH 隧道 - MySQL 服务器已消失
- ios - 带有图像的单元格的 UICollectionView 初始滚动滞后