O erro é apenas esse: Ele não está concatenando meu array de User. Se eu trocar o metodo concat por push ele da o seguinte erro: "Argument of type User is not assignable to parameter of type User" listUser.service.ts
import { HttpClient} from '@angular/common/http';
import { Injectable } from '@angular/core';
import * as $ from 'jquery';
import { User } from '../Users/User/User';
const API = 'https://randomuser.me/api';
@Injectable({ providedIn: 'root' })
export class ListUserService {
users: User[] = [];
constructor(private http: HttpClient) {}
GetUser(){
return this.http
.get<User>(API);
}
delay(milliseconds: number, count: number) {
return new Promise<number>(resolve => {
setTimeout(() => {
resolve(count);
}, milliseconds);
});
}
// async function always returns a Promise
async ListUsers(){
// var users: User[] = [];
for (let i = 0; i < 101; i++) {
// await is converting Promise<number> into number
const count:number = await this.delay(50, i);
this.GetUser().subscribe(user =>{
console.log(user);
this.users.push(user);
});
}
}
getUsers(){
return this.users;
}
user.component.ts
import { Component, OnInit, OnChanges } from '@angular/core';
import { ListUserService } from '../services/listUser.service';
import { User } from '../Users/User/User';
@Component({
selector: 'app-lista',
templateUrl: './lista.usuarios.component.html',
styleUrls: ['./lista.usuarios.component.css']
})
export class ListaUsuariosComponent implements OnInit {
users: User[] = [];
constructor(private listUser: ListUserService) { }
ngOnInit() {
this.listUser.ListUsers();
}
load() {
//erro aqui!!!
this.users.concat(this.listUser.getUsers());
console.log(this.users);
}
}
user.component.html
<div class="text-center" (click)="load()" >
<button class="btn btn-primary">Load more</button>
</div>