【问题标题】:Get value of HTTPClient in Angular在 Angular 中获取 HTTPClient 的值
【发布时间】:2019-12-13 22:07:45
【问题描述】:

data-sharing.service.ts

public httpGetAll(owner: any) {
    return this.http.get(`${this.url}/${owner}`, this.httpOptions).pipe(
      catchError(e => {
        throw new Error(e);
      })
    );
  }


public httpGetAllBy(id: number, byId:string, owner: any) {
    return this.httpGetAll(owner).subscribe(data => {
      Object.keys(data).map(function(key) {
        return data[key].filter(x => x[byId] === id);
      });
    })

station.service.ts

getStationsByOrg(id) {
    return this.dataSharing.httpGetAllBy(id, 'orgId', 'station');
  }

ma​​nagestation.component.ts

getStationsByOrg(id) {
    this.sta = this.staService.getStationsByOrg(id);
  }

ma​​nagestation.component.html

<ion-content class="body">
  <ion-button expand="block" (click)="onAddStation()">Add Station
    <ion-icon name='add' slot="end"></ion-icon>
  </ion-button>
  <ion-list>
    <ion-item *ngFor="let s of sta">
      <ion-label>{{ s.name }}</ion-label>
      <ion-label>{{ s.orgId }}</ion-label>
      <ion-icon name='create' slot="end" (click)="onEditStation(s.id)"></ion-icon>
      <ion-icon name='trash' slot="end" (click)="onDeleteStation(s.id)"></ion-icon>
    </ion-item>
  </ion-list>
</ion-content>

错误

错误错误:“[对象对象]” Angular 11 core.js:4002

如何获取 managestation.component.tshttpGetAllBy 的值并将其分配给 this.sta。 ?

【问题讨论】:

    标签: angular typescript ionic-framework


    【解决方案1】:

    如下修改你的文件

    data-sharing.service.ts

    public httpGetAllBy(id: number, byId:string, owner: any) {
        return new Promise((resolve, reject) => {
         this.httpGetAll(owner)
          .subscribe(data => {
            let filterdData = Object.keys(data).map(function(key) {
             return data[key].filter(x => x[byId] === id);
            });
            resolve(filterdData);
          })
        });
    }
    

    ma​​nagestation.component.ts

    getStationsByOrg(id) {
        this.staService.getStationsByOrg(id)
        .then((allData) => {
           this.sta = allData;
        })
    
      }
    

    【讨论】:

    • 我有以下 getStationsByOrg() getUsersByOrg() getUsersByStation() getTanksByStation() getPumpsByStation() getPumpsByTank() 所以我想让 httpGetAllBy() 有过滤功能所以我不必重复以上所有功能
    • 使用 Promise 方式修改答案。请再次检查。
    • src/app/members/managestation/managestation.component.ts(65,10) 中的错误:错误 TS2739:类型“{}”缺少“Station”类型中的以下属性:id,名称,orgId,使用 console.log(allData) 的状态;但在 this.sta = allData; 中得到上面的错误
    【解决方案2】:

    您正在返回订阅,但不是数据。选项:

    • 2) 返回 observable 本身:
      public httpGetAllBy(id: number, byId:string, owner: any) {
          return this.httpGetAll(owner);
      }
      getStationsByOrg(id) {
          return this.dataSharing.httpGetAllBy(id, 'orgId', 'station').subscribe(data=>{
            const var = Object.keys(data).map(function(key) {
              return data[key].filter(x => x[byId] === id);
            });
      //your logic in station.service.ts
      };
        }
      
      2) 使用 async/await 获取数据:
      public async httpGetAllBy(id: number, byId:string, owner: any) {
          return await this.httpGetAll(owner).toPromise().then(data => {
            return Object.keys(data).map(function(key) {
              return data[key].filter(x => x[byId] === id);
            });
          })
      getStationsByOrg(id) {
          return this.dataSharing.httpGetAllBy(id, 'orgId', 'station').then(data=>{
      //your logic in station.service.ts
      };
      

    【讨论】:

      【解决方案3】:

      你应该在你的服务中返回一个 Observable 并通过 pipe() 映射它。

      public httpGetAllBy(id: number, byId:string, owner: any) {
          return this.httpGetAll(owner).pipe(
              map( (data => {
                  Object.keys(data).map(function(key) {
                      return data[key].filter(x => x[byId] === id);
                  });
              }))
          );
      }
      

      然后你就可以在你的组件中订阅了。

      【讨论】:

      • src/app/_services/data-sharing.service.ts(177,9) 中的错误:错误 TS2552:找不到名称“地图”。您指的是“地图”吗?
      • 不,你应该从 RxJS 导入它
      猜你喜欢
      • 2018-11-14
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2019-03-04
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      相关资源
      最近更新 更多