Vue+Axios小案例【天气预报查询】

一、效果演示

在这里插入图片描述

二、完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查询天气预报</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        #title{
            text-align: center;
        }
        #app {
            margin-top: 50px;
            text-align: center;
        }

        input {
            height: 30px;
            line-height: 30px;
        }

        #btn {
            display: inline-block;

            background-color: firebrick;
            color: white;
            width: 70px;
            height: 35px;
            line-height: 35px;
            border-radius: 2px;
            margin-left:10px;
        }

        ul {

            width: 80%;
            height: 150px;
            margin: 50px auto;
        }

        li {
            width: 16%;
            float: left;
            list-style: none;
            margin: auto 20px;
            height: 100%;
            background-color: aliceblue;
        }
        .weatherInfo{
            width:100%;
            height:50px;
            line-height: 50px;
        }
        .first{
            color:black;
        }
        .second{
            color:red;
        }
        .third{
            color:deepskyblue;
        }
        .low{
            color:grey;
        }

    </style>
</head>

<body>
    <h3 id="title">天气预报查询</h3>
    <div id="app">
        <input type="text" placeholder="请输入要查询的城市名称" @keyup.enter="queryWeather" v-model="city">
        <div @click="queryWeather" id="btn">查询</div>
        <ul id="weather">
            <li v-for="w in weatherArr">
                <div class="weatherInfo first">
                    {{w.date}} {{w.type}}
                </div>
                <div class="weatherInfo second">
                    <span class="low">{{w.low}}</span> ~ <span class="high">{{w.high}}</span>
                </div>
                <div class="weatherInfo third">
                    {{w.fengxiang}} {{w.fengli.substr(9,2)}}
                </div>
            </li>
        </ul>
    </div>
</body>
<script>
    // 请求地址:http://wthrcdn.etouch.cn/weather_mini
    // 请求方法:get
    // 请求参数:city (要查询的城市名称)
    // 响应内容:天气信息
    new Vue({
        el: "#app",
        data: {
            city: '',
            weatherArr: []
        },
        methods: {
            queryWeather() {
                var ct = this.city;
                var t = this;
                axios.get("http://wthrcdn.etouch.cn/weather_mini", { params: { "city": ct } }).then(
                    function (response) {
                        t.weatherArr = response.data.data.forecast;
                        //console.log(response.data.data.forecast)
                        var fengli = response.data.data.forecast[0].fengli
                        //alert(fengli.substr(9,2))
                    }, function (error) {

                    }
                )
            }
        }
    })
</script>

</html>

三、相关说明

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迟到_啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值