小编Tha*_*thy的帖子

同一请求的多个 API 调用

我想使用传递不同 id 的同一端点进行多个 HTTP 调用。有没有更好的方法从 UI 处理这个问题。目前无法更改后端,有更好的方法吗?

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, forkJoin } from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: 'app/app.component.html'
})
export class AppComponent {
  loadedCharacter: {};
  constructor(private http: HttpClient) {}

  ngOnInit() {
    let character1 = this.http.get('https://swapi.co/api/people/1');
    let character2 = this.http.get('http://swapi.co/api/people/2');

    forkJoin([character, character2]).subscribe(results => {
      // results[0] is our character
      // results[1] is our character2
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript rxjs angular

2
推荐指数
2
解决办法
2万
查看次数

查找对象数组中的计数总数

包括有多少潜在选民年龄在 18-25 岁之间,有多少人在 26-35 岁之间,有多少人在 36-55 岁之间,以及每个年龄段有多少人实际投票。包含此数据的结果对象应具有 6 个属性。

var voters = [
    {name:'Bob' , age: 30, voted: true},
    {name:'Jake' , age: 32, voted: true},
    {name:'Kate' , age: 25, voted: false},
    {name:'Sam' , age: 20, voted: false},
    {name:'Phil' , age: 21, voted: true},
    {name:'Ed' , age:55, voted:true},
    {name:'Tami' , age: 54, voted:true},
    {name: 'Mary', age: 31, voted: false},
    {name: 'Becky', age: 43, voted: false},
    {name: 'Joey', age: 41, voted: true},
    {name: 'Jeff', age: 30, voted: true},
    {name: 'Zack', age: 19, …
Run Code Online (Sandbox Code Playgroud)

javascript

0
推荐指数
1
解决办法
783
查看次数

标签 统计

javascript ×2

angular ×1

rxjs ×1