<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>查天气</title>
</head>
<body>
<div id="app">
<input type="text" v-model="city" @keyup.enter="getWeather">
<br>
<a href="javascript:;" @click="changeCity('北京')">北京</a>
<a href="javascript:;" @click="changeCity('上海')">上海</a>
<br>
<hr>
<span v-text="distCity"></span>
<ul>
<li v-for="weather in weatherList">
{{weather.date}}
{{weather.high}}
{{weather.low}}
{{weather.fengli}}
{{weather.fengxiang}}
{{weather.type}}
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios@0.21.1/dist/axios.min.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
city: '',
distCity: '',
weatherList: []
},
methods: {
getWeather: function () {
let that = this;
axios.get('http://wthrcdn.etouch.cn/weather_mini?city=' + this.city)
.then(function (response) {
console.log(response.data.data.forecast);
that.distCity = response.data.data.city;
that.weatherList = response.data.data.forecast
}).catch(function (err) {
console.log(err);
})
},
changeCity: function (city) {
this.city = city;
this.getWeather();
}
}
})
</script>
</body>
</html>