首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将从json文件中剔除的多个对象组合起来,并作为可观察的返回。

将从json文件中剔除的多个对象组合起来,并作为可观察的返回。
EN

Stack Overflow用户
提问于 2019-06-07 00:40:00
回答 1查看 27关注 0票数 0

我想将对象this.webCustomAlertthis.webCustomAuththis.webCustomEcommerce组合成一个对象。它们都有共同的属性。所以我想从以下几个方面着手:

webCustomAuth

代码语言:javascript
复制
[{ "type":"google-login", "cost":30, "buildTime":2 },{ "type":"twitter-login", "cost":30, "buildTime":2 },{ "type":"facebook-login", "cost":30, "buildTime":2 }]

webCustomAlert

代码语言:javascript
复制
[{ "type":"desktop-notifications", "cost":30, "buildTime":2 },{ "type":"web-notification-page", "cost":30, "buildTime":2 },{ "type":"web-notification-settings", "cost":30, "buildTime":2 }]

webCustomEcommerce

代码语言:javascript
复制
[{ "type":"affiliate-url", "cost":30, "buildTime":2 },{ "type":"coupons", "cost":30, "buildTime":2 },{ "type":"discounts", "cost":30, "buildTime":2 }]

webCustomFeatures

代码语言:javascript
复制
[{ "type":"affiliate-url", "cost":30, "buildTime":2 },{ "type":"coupons", "cost":30, "buildTime":2 },{ "type":"discounts", "cost":30, "buildTime":2 },{ "type":"desktop-notifications", "cost":30, "buildTime":2 },{ "type":"web-notification-page", "cost":30, "buildTime":2 },{ "type":"web-notification-settings", "cost":30, "buildTime":2 },{ "type":"google-login", "cost":30, "buildTime":2 },{ "type":"twitter-login", "cost":30, "buildTime":2 },{ "type":"facebook-login", "cost":30, "buildTime":2 }]

这是我的代码:

代码语言:javascript
复制
  public webCustomFeatures: any;
  public webCustomAlert: any;
  private webCustomAlertDataPath = './assets/data/web-custom-alert.json';
  public webCustomAuth: any;
  private webCustomAuthDataPath = './assets/data/web-custom-auth.json';
  public webCustomEcommerce: any;
  private webCustomEcommerceDataPath = './assets/data/web-custom-ecommercejson';

  constructor(
    public httpClient: HttpClient
  ) {
    this.webCustom = this.httpClient.get(this.webCustomDataPath);
    this.webCustomAlert = this.httpClient.get(this.webCustomAlertDataPath);
    this.webCustomAuth = this.httpClient.get(this.webCustomAuthDataPath);
    this.webCustomEcommerce = this.httpClient.get(this.webCustomEcommerceDataPath);
  }

  public getAllWebCustomAlert(): Observable<any> {
    return this.webCustomAlert;
  }

  public getAllWebCustomAuth(): Observable<any> {
    return this.webCustomAuth;
  }

  public getAllWebCustomEcommerce(): Observable<any> {
    return this.webCustomEcommerce;
  }

  public getAllWebCustomFeatures(): Observable<any> {
    const webCustomFeatures = [];
    webCustomFeatures.push(this.webCustomAlert);
    webCustomFeatures.push(this.webCustomAuth);
    webCustomFeatures.push(this.webCustomEcommerce);
    return webCustomFeatures;
  }

我一直收到错误消息Type 'any[]' is not assignable to type 'Observable<any>'. Property '_isScalar' is missing in type 'any[]'.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-07 01:32:34

您正在返回一个不可观测的可观测数据数组,您想要combineLatest

代码语言:javascript
复制
public getAllWebCustomFeatures(): Observable<any> {
  return combineLatest(this.webCustomAlert, this.webCustomAuth, this.webCustomEcommerce);
}

将发出和数组,一旦所有三个发出,那么您可以将数组分解为值。

代码语言:javascript
复制
getAllWebCustomFeatures().subscribe(([webCustomAlert, webCustomAuth, webCustomEcommerce]) => {
  //Do stuff with the destructured values here
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56486673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档