По какой-то причине мой внешний вызов API работает только 80% времени, поэтому, если он терпит неудачу, я хотел бы попытаться вызвать его еще 2-3 раза, прежде чем выдать ошибку. Это возможно?
Ниже приведен код из моих файлов компонентов и сервисов. Ошибка, которую я выдаю, находится в моем файле компонента с функцией getCars (). API, который я вызываю, размещен на Heroku.
Компонент
import { Component, OnInit } from '@angular/core';
import { CarsService, Car } from '../cars.service';
@Component({
selector: 'app-car',
templateUrl: './car.component.html',
styleUrls: ['./car.component.css']
})
export class CarComponent implements OnInit {
cars: Car[];
constructor(
public carService: CarsService
) {
this.getCars();
}
getCars(){
this.carService.getCars().subscribe(
data => {
this.cars = data;
},
error => {
alert("Could not retrieve a list of cars");
}
)
};
Услуги
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment';
export interface Car {
make: string;
model: string;
year: string;
}
@Injectable({
providedIn: 'root'
})
export class CarsService {
baseUrl = environment.baseUrl;
constructor(
public http: HttpClient
) { }
getCars() {
let url = this.baseUrl + '/api/car'
return this.http.get<Car[]>(url);
}
}