我只想从组件的 ember 服务中的 json 请求中获取价值。这是我的代码
预测天气.js(服务)
import Ember from 'ember';
export default Ember.Service.extend({
findWeatherCurrent:function (lat,lng) {
return Ember.$.getJSON('https://api.darksky.net/forecast/22b106924ed7e3bcde76608f7f064585/'+ lat +','+lng+'?exclude=minutely,hourly,daily,flags&units=si');
},
findWeatherDaily:function (lat,lng) {
return Ember.$.getJSON('https://api.darksky.net/forecast/22b106924ed7e3bcde76608f7f064585/'+ lat +','+lng+'?exclude=minutely,hourly,currently,flags&units=si');
},
findWeatherHourly:function (lat,lng) {
return Ember.$.getJSON('https://api.darksky.net/forecast/22b106924ed7e3bcde76608f7f064585/'+ lat +','+lng+'?exclude=minutely,daily,currently,flags&units=si');
}
});
天气显示.js(组件)
import Ember from 'ember';
export default Ember.Component.extend({
forecastWeather:Ember.inject.service(),
willRender(){
let lat = this.get('lat');
let lng = this.get('lng');
this.get('forecastWeather').findWeatherCurrent(lat,lng).then(data => {
this.set('currents', data);
console.log(data);
});
}
});
json响应
{
"latitude": 37.8267,
"longitude": -122.4233,
"timezone": "America/Los_Angeles",
"offset": -7,
"currently": {
"time": 1489488513,
"summary": "Clear",
"icon": "clear-night",
"nearestStormDistance": 47,
"nearestStormBearing": 87,
"precipIntensity": 0,
"precipProbability": 0,
"temperature": 13.54,
"apparentTemperature": 13.54,
"dewPoint": 8.59,
"humidity": 0.72,
"windSpeed": 0.87,
"windBearing": 46,
"visibility": 12.46,
"cloudCover": 0.08,
"pressure": 1016.58,
"ozone": 279.62
}
}
天气显示.hbs
<p id="word_on_change" class="font_black font-white word">{{currents.currently.windSpeed}}</p>
我只是想将风速值从 json 传递给 hbs 模板,但它不起作用。任何人都可以解决这个问题:(