gpt4 book ai didi

vue.js - 更改 Vuejs 中的 v-model 输入值时,数据不会动态更新

转载 作者:行者123 更新时间:2023-12-03 06:46:21 25 4
gpt4 key购买 nike

我正在用这个 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>
当我换到蒙特利尔、多伦多、渥太华、艾伯塔等城市时,我正在尝试……它会相应地改变预测。

最佳答案

您没有 currentCity 的事件处理程序变化。因此,您的代码将在初始加载(即在 mounted 上)运行并更改为 currentCity不会改变任何天气数据。

您需要添加 @change到输入并在每次更改时获取新的 api 数据。

下面是示例代码

new Vue({
el: '#app',
data() {
return {
// current weather
currentWeather: null,
// current city
currentCity: 'Montreal',
// current country
currentCountry: 'ca',
unit: 'imperial'
}
this.$set(this.currentCity);
},
methods: {
getWeather() {
// Make axios request to open weather api
fetch('https://api.openweathermap.org/data/2.5/weather?q=' + this.currentCity + ',' + this.currentCountry + '&appid=fe435501a7f0d2f2172ccf5f139248f7&units=' + this.unit + '')
.then(res => res.json()).then(data => {
// takes response object & stores it in currentWeather
this.currentWeather = data;

})
.catch(function(error) {
// handle error
console.log(error);
})
}
},
mounted() {
this.getWeather();
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="app">
<div class="home">
<p>A weather app built Vuejs & Open Weather App. Made by Manuel Abascal</p>
Search: <input type="text" v-model.lazy="currentCity" @change="getWeather">
<div class="forecast" v-if="currentWeather && currentWeather.cod == 200">
<!-- Forecast stat values -->
<h2>Right now:</h2>
<div><strong>City:</strong> {{ currentWeather.name }}</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 v-else>
"{{ currentCity }}" is not found
</div>
</div>
</div>

关于vue.js - 更改 Vuejs 中的 v-model 输入值时,数据不会动态更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56546715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com