码迷,mamicode.com
首页 > 移动开发 > 详细

axios 查询天气的案例

时间:2020-07-23 01:53:14      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:nbsp   column   turn   20px   info   hot   axios   pen   ios   

天气查询接口:

功能:

  • 点击“北京”、“上海”、“上海”、“深圳”按钮可查询对应城市的5天天气
  • 输入城市名,回车或点击“搜索”按钮 也可查询城市的5天天气

vue语法:

  • v-for、v-model、v-on、created()

 结果图:

技术图片

 

 

html代码:

技术图片
<body>
    <div id="app" class="wrap">
        <!-- 显示logo -->
        <div class="logo fiex">
            <img src="./img/weather.jpg" alt="logo" class="imgLogo"> 
        </div>
        <!-- 搜索 -->
        <div class="search_form">
            <input type="text" v-model="city" @keyup.enter="searchWeather"
             class="input_txt" placeholder="请输入查询的天气"/>
             <input type="button" class="input_sub" @click="searchWeather" value="搜索" >
        </div>
        <!-- 城市按钮 -->
        <div class="hotkey">
            <a @click="changeCity(‘北京‘)">北京</a>&nbsp;&nbsp;
            <a @click="changeCity(‘上海‘)">上海</a>&nbsp;&nbsp;
            <a @click="changeCity(‘广州‘)">广州</a>&nbsp;&nbsp;
            <a @click="changeCity(‘深圳‘)">深圳</a>
        </div>
        <!-- 显示天气 -->
        <div>
            <ul class="weather_list">
                <li v-for="item in weatherList">
                    <div class="info_type"><span class="iconfont">{{item.type}}</span></div>
                    <div class="info_temp">
                        <b>{{item.low}}</b>
                        ~
                        <b>{{item.high}}</b>
                    </div>
                    <div class="info_date"><span>{{item.date}}</span></div>
                </li>
                <!-- 测试 -->
                <!-- <li v-for="item in [0,1,2,3,4]">
                    <div class="info_type"><span class="iconfont">多云</span></div>
                    <div class="info_temp">
                        <b>低温18`C</b>
                        ~
                        <b>高温25·C</b>
                    </div>
                    <div class="info_date"><span>31日星期三</span></div>
                </li> -->
            </ul>
        </div>
    </div>
</body>
html代码

js代码:

技术图片
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app=new Vue({
            el:"#app",
            data() {
                return {
                    city:"揭阳",
                    weatherList:[]
                }
            },
            methods: {
                searchWeather:function(){
                      var _this=this;
                      console.log("天气查询");
                      console.log(_this.city);
                      axios.get("http://wthrcdn.etouch.cn/weather_mini?city="+_this.city)
                      .then(function(response){
                        console.log(response.data);
                        _this.weatherList=response.data.data.forecast;
                      },function(err){
                        console.log(err);
                      })
                },
                changeCity:function(city){
                    this.city=city;
                    this.searchWeather();
                }
                
            },
            created:function(){
                    this.searchWeather();
                }
        })
    </script>
js代码

css代码:

技术图片
<style>
        .imgLogo{
            height: 80px;
            width: 200px;
            margin:0 auto;
        }
        .logo{
            width: 200px;
            margin: 0 auto;
            margin-top: 100px;
            align-items: center;
            flex-direction: column-reverse;
            margin-bottom: 5px;
        }
        .search_form{                   
            width: 600px;
            margin: 0 auto;
        }
        .input_txt{
            padding-left: 8px;          
            height: 25px;
            width: 500px;
            border: 1.5px solid rgb(81, 167, 248);
            outline: none;
            /* margin-top: 0px; */
        }
        .input_sub{
            color: white;
            font-weight: bolder;
            height:30px;
            width: 60px;
            background-color: rgb(81, 167, 248);
            border: none;
            display: inline-block;
            margin-left: -5px;
            display: block;
            float: left;
        }
        .hotkey{
            width: 600px;
            margin: 0 auto;
        }
        .weather_list{
            width:820px;
            margin: 0 auto;
            margin-top: 20px;
        }
        .weather_list li{
            float: left;
            list-style-type: none;
            border-right: 1px solid gray;
            border-left: 1px solid gray;
            opacity: 0.6;
        }
        .info_type{
            font-size: 33px;
            font-weight: normal;
            text-align: center;
            color: orange;
        }
        .info_temp{
            padding: 10px;
            font-size: 13px;
            color: orange;
            font-weight: lighter;
        }
        .info_date{
            text-align: center;
            font-size: 15px;
        }
    </style>
css代码

axios 查询天气的案例

标签:nbsp   column   turn   20px   info   hot   axios   pen   ios   

原文地址:https://www.cnblogs.com/technicist/p/13362401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!