Outils pour utilisateurs

Outils du site


informatique:java-script:angular:requete_api

Requete Api

Date de création : 2020/12/01 22:08

 

dans un service:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
 
@Injectable({
  providedIn: 'root'
})
export class OWMapService {
 
  url = 'https://api.openweathermap.org/data/2.5';
 
  constructor(
    private _http: HttpClient
  ) { }
 
  async getCurrentWeather(lat, long) {
    const datas = await this._http.get(this.url + `/onecall?lat=${lat}&lon=${long}&appid=${environment.appId}&units=metric`)
    .toPromise()
    .catch(err => err);
    return datas;
  }
}
 
 
 
Ensuite on fait une instance du service dans une variable privé:
</code JavaScript>
constructor(
    private _owm: OWMapService
  ) { }

et on peut directement utiliser le service avec une fonction async pour attendre la reponse:

const {coords = {}} = await this._owm.getCurrentWeather(coords.latitude, coords.longitude);
informatique/java-script/angular/requete_api.txt · Dernière modification : 2022/04/10 17:44 de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki