Я создаю погодное приложение с помощью этого Weather API. Я пытаюсь добавить значение поля <input>
, которое при изменении названия города обновляет прогноз других значений.
Я создал поле <input>
, которое обновляет значение города, и оно должно соответствующим образом обновлять прогноз погоды. Я знаю, что v-model
работает, но это не меняет результатов. Только когда я жестко запрограммировал другой город в Vue-instance
, данные обновляют изменения.
<template>
<div class="home">
<h1>{{ msg }}</h1>
<p>A weather app built Vuejs & Open Weather App. Made by Manuel Abascal</p>
<input type="text" v-model.lazy="currentWeather.name">
<div class="forecast">
<div v-if="this.currentWeather">
<!-- Forecast stat values -->
<h2>Right now:</h2>
<div><strong>City:</strong> {{ currentCity }}</div>
<div><strong>Longitude: </strong> {{ currentWeather.coord.lon }}</div>
<div><strong>Latitude: </strong> {{ currentWeather.coord.lat }}</div>
<div><strong>Weather condition </strong> {{ currentWeather.weather[0].description }}</div>
<div><strong>Temperature Mid: </strong> {{ currentWeather.main.temp }} Farenheit</div>
<div><strong>Temperature Max: </strong> {{ currentWeather.main.temp_max}} Farenheit</div>
<div><strong>Temperature Min: </strong> {{ currentWeather.main.temp_min}} Farenheit</div>
<div><strong>Humidity: </strong> {{ currentWeather.main.humidity }}%</div>
<div><strong>Wind: </strong> {{ currentWeather.wind.speed }} mph</div>
</div>
</div>
</div>
</template>
<script>
// import Axios
import axios from "axios"
export default {
name: "Home",
props: {
msg: String,
},
data(){
return {
// current weather
currentWeather: null,
// current city
currentCity: 'Montreal',
// current country
currentCountry: 'ca',
unit: 'imperial'
}
this.$set(this.currentCity);
},
mounted(){
// Make axios request to open weather api
axios.get('https://api.openweathermap.org/data/2.5/weather?q='+this.currentCity+','+this.currentCountry+'&appid=fe435501a7f0d2f2172ccf5f139248f7&units='+this.unit+'')
.then((response) => {
// takes response object & stores it in currentWeather
this.currentWeather = response.data
})
.catch(function (error) {
// handle error
console.log(error);
})
}
};
</script>
<style scoped lang="scss">
</style>
Я пытаюсь переехать в такие города, как Монреаль, Торонто, Оттава, Альберта и т. Д., Это меняет прогноз соответственно.