javascript - Angular 雅虎天气 API
问题描述
我对 Yahoo Weather API 有疑问。我应该如何请求从 Yahoo Weather API 获取数据?
import { Injectable } from '@angular/core';
import {Observable} from 'rxjs';
import {HttpClient} from '@angular/common/http';
@Injectable()
export class WeatherService {
constructor(private http: HttpClient) { }
getWeatherForecast(city: string): Observable<any> {
const url = 'https://query.yahooapis.com/v1/public/yql?q=select wind from
weather.forecast where woeid=2460286';
return this.http.get(url);
}
}
解决方案
这是一个简单的例子,但我希望它对你有所帮助。我将axios
在这里使用一个名为的库:
const url = "https://query.yahooapis.com/v1/public/yql?q=select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text='Sunderland') and u='c'&format=json";
const getWeatherData = () => {
axios.get(url)
.then(res => console.log(res.data.query.results.channel.item.condition))
.catch(err => console.log(err.data))
}
getWeatherData();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.12.0/axios.min.js"></script>
推荐阅读
- javascript - Stencil.js - 事件监听器的解释
- swiftui - SwiftUI - 从外部调用视图中的函数
- python - 为什么 Google Cloud Dataflow 无法扩展到启用自动缩放且没有配额限制的目标工作人员
- javascript - 设计一个无法看到文本的透明标题栏
- php - API平台自定义get操作
- javascript - 子组件更新时反应不重新渲染
- git - 如何确保我的 git 预提交脚本不会被愚弄?
- r - 如何从线性模型中提取系数而不在 R 中重复我的代码?
- javascript - Ember 输入类型数字只允许在 Decimal 后输入 2 位数字
- flutter - 在合并请求中在 GitLab CI 上格式化 Flutter 代码库