I'm new to Vue and am working on an app to get the weather from a weather api.
The code below works if I leave axios.get() in Content.vue as it is below.
I want to move it to a separate file (WeatherAPI.js) and call it from there. If I comment out axois.get() call in Content.vue and uncomment this.todaysWeather = WeatherAPI.returnTodaysWeather(); it doesn't work. I do see the data logged to the console from WeatherAPI.js so it's getting it, but it doesn't seem to return it correctly. I see undefined in the console log from Content.vue.
Content.vue
<template>
<div class="container-fluid">
<div class="row answerRow text-center">
<div class="col">
</div>
</div>
<div class="row">
<div class="col">
<weather-today :todaysWeather="todaysWeather"></weather-today>
</div>
<div class="col">
Column2
</div>
</div>
</div>
</template>
<script>
import Today from '../weather/Today.vue'
import WeatherAPI from '../data/WeatherAPI.js'
import axios from 'axios'
const apiURL = "http://api.openweathermap.org/data/2.5/weather";
const apiKey = "MyKeyHere";
const zipCode = "11111,us";
const dailyWeatherUrl = apiURL + "?zip=" + zipCode + "&appid=" + apiKey;
export default {
mounted(){
this.getTodaysWeather();
console.log(this.todaysWeather)
},
data () {
return {
todaysWeather: {name: "testname"}
}
},
components: {
'weather-today': Today
},
methods: {
getTodaysWeather () {
//this.todaysWeather = WeatherAPI.returnTodaysWeather();
axios.get(dailyWeatherUrl)
.then((response) => {
console.log(response.data);
this.todaysWeather = response.data;
})
.catch(function (error) {
console.log(error);
});
}
}
}
</script>
<style>
.answerRow{
background-color: yellow;
}
</style>
Today.vue
<template>
<div class="container row1">
<div class="row">
<div class="col">
<h2>Todays Weather</h2>
</div>
</div>
<div class="row">
<div class="col">
City: {{ todaysWeather.name }}
</div>
</div>
</div>
</template>
<script>
import WeatherAPI from '../data/WeatherAPI.js'
export default {
props: ['todaysWeather'],
mounted() {
console.log(this.todaysWeather)
}
}
</script>
<style>
.row1{
background-color: #3498DB;
}
</style>
WeatherAPI.js
const apiURL = "http://api.openweathermap.org/data/2.5/weather";
const apiKey = "MyKeyHere";
const zipCode = "11111,us";
const dailyWeatherUrl = apiURL + "?zip=" + zipCode + "&appid=" + apiKey;
import axios from 'axios';
export default {
data(){
return {
todaysWeather: {}
}
},
returnTodaysWeather () {
axios.get(dailyWeatherUrl)
.then((response) => {
console.log(response.data);
this.todaysWeather = response.data;
console.log(this.todaysWeather);
return this.todaysWeather;
})
.catch(function (error) {
console.log(error);
});
}
}