Vue天气预报——axios数据请求

zhuanbike 7月前 258

vue确实高效,这天气预报页面要是换react,估计得写半天。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>天气预报-Vue</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="weather">
    <div id="sbox">
        <input type="text" v-model="city" placeholder="请输入查询城市" @keyup.enter="searchWeather">
        <button @click="searchWeather">查 询</button>
    </div>
    <div>
        <a href="javascript:;" @click="changeCity('北京')">北京</a>
        <a href="javascript:;" @click="changeCity('上海')">上海</a>
        <a href="javascript:;" @click="changeCity('杭州')">杭州</a>
        <a href="javascript:;" @click="changeCity('深圳')">深圳</a>
    </div>
    <h2>{{city}}</h2>
    <ul>
        <li v-for="item in weatherList">
            <div>
                <span>{{item.type}}</span>
            </div>
            <div>
                <b>{{item.low}}</b>~<b>{{item.high}}</b>
            </div>
            <div><span>{{item.date}}</span></div>
        </li>
    </ul>
</div>
    <script>
        const vm = new Vue({
            el:"#weather",
            data:{
                city:'',
                weatherList:[]
            },
            methods:{
              searchWeather:function(){
                  var that=this;
                  axios.get('http://wthrcdn.etouch.cn/weather_mini?city='+this.city)
                  .then(function(response){
                      that.weatherList=response.data.data.forecast
                  })
                  .catch(function(err){})
              },
              changeCity:function (city) {
                  this.city=city;
                  this.searchWeather()
              }
            }            
        }
        )
    </script>
</body>
</html>


最新回复 (0)
发新帖