我只是想在我的應用程序登陸頁面/ index/application.hbs中顯示我的經度。我很尷尬地說,我一直在這工作多久!誰能幫我?Ember:在組件中使用服務
//geoservice.js
import Ember from 'ember';
export default Ember.Service.extend({
longitude: function(position){
return position.coords.longitude;
},
latitude: function(position){
return position.coords.latitude;
}
});
//geo-component.js
import Ember from 'ember';
export default Ember.Component.extend({
geoservice: Ember.inject.service(),
myLongitude: function(){
if (navigator.geolocation) {
return this.get('geoservice').longitude(navigator.geolocation.getCurrentPosition());
} else {
return "Geolocation is not supported by this browser.";
}
}.on('init'),
});
//application.hbs
<h2 id="title">Welcome to Ember</h2>
{{outlet}}
{{geo-component.myLongitude}}
地理位置API是通過[navigator.geolocation]出版(https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation)對象。 –