vue实现一个简单的天气查询

效果:
在这里插入图片描述
代码:

<html>
<head>
    <meta charset="utf-8">
    <title>天气通</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        div > span {
            float: left
        }

        p {
            text-align: center;
        }

        #test {
            width: 70%;
            margin: 0 auto
        }

        h2 {
            text-align: center;
            margin-top: 20px;
        }

        .cities {
            width: 20%;
            margin: 0 auto;
            font-size: 13px;
        }

        .weather_parent {
            display: block;
            margin-top: 50px
        }

        .weather_item {
            width: 19%;
            display: inline-block
        }
    </style>
</head>
<body>


<div id="test">
    <h2>天气通</h2>

    <div style="width: 20%;margin:0 auto">
        <input placeholder="请输入城市名称" v-model="city" @keyup.enter="getData()"/>
        <span style="float:right" @click="getData()">搜索</span>
    </div>

    <div class="cities">
        <span style="margin:6px" v-for="item in cities" @click="getData(item)">{{item}}</span>
    </div>

    <div class="weather_parent">
        <div class="weather_item" v-for="item in datas">
            <div style="width: 80px;margin:0 auto">
            <img :src="item.icon" style="height:25px;width:35px;margin-right: 10px"/><span style="line-height: 25px;float:right">{{item.type}}</span>
            </div>
            <p>
            {{item.low}}  {{item.high}}
            </p>
            <p>
            {{item.date}}
            </p>
        </div>
    </div>
</div>
<script>
    let xmlhttp;
    if (window.XMLHttpRequest) {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // IE6, IE5 浏览器执行代码
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    const app = new Vue({
        el: "#test",
        data: {
            response: "",
            city: "",
            cities: ["北京", "杭州", "上海"],
            datas: [],
            //http://www.weather.com.cn/m/i/icon_weather/42x30/d00.gif
            icons:["晴","多云","阴","阵雨","雷阵雨","雷阵雨伴有冰雹","雨夹雪","小雨","中雨","大雨","暴雨","大暴雨","特大暴雨",
                "阵雪","小雪","中雪","大雪","暴雪","雾","冻雨","沙尘暴","小雨转中雨","中雨转大雨","大雨转暴雨","暴雨转大暴雨","大暴雨转特大暴雨",
                "小雪转中雪","中雪转大雪","大雪转暴雪","浮尘","扬沙","强沙尘暴","霾"
            ]

        },
        methods: {
            getData: function (city) {
                if (city === undefined) {
                    city = this.city
                }
                if (app.cities.indexOf(city) >= 0) {
                    app.cities.splice(app.cities.indexOf(city), 1)
                }
                app.cities.unshift(city);

                if (app.cities.length > 5) {//最多存5条
                    app.cities.splice(5)
                }
                
                
                localStorage.setItem("cities", JSON.stringify(app.cities));

                const that = this;
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                        //document.getElementById("test").innerHTML=xmlhttp.responseText;
                        app.response = xmlhttp.responseText;
                        console.log(xmlhttp.responseText);
                        that.datas = JSON.parse(xmlhttp.responseText).data.forecast;
                        for (index =0;index < app.datas.length;++index) {
                            let position = app.icons.indexOf(app.datas[index].type)<=9?"0"+app.icons.indexOf(app.datas[index].type):""+app.icons.indexOf(app.datas[index].type)
                            app.datas[index].icon = "http://www.weather.com.cn/m/i/icon_weather/42x30/d"+ position +".gif"
                        }
                    }
                };
                xmlhttp.open("GET", "http://wthrcdn.etouch.cn/weather_mini?city=" + city, true);
                xmlhttp.send();
            }
        }
    });

    /**
     * 获取缓存的历史查询
     * */
    document.addEventListener("DOMContentLoaded", function(){
        // localStorage.clear();
        let cities = JSON.parse(localStorage.getItem("cities"));
        if (cities != null) {
            app.cities = cities
        }
    });
</script>
</body>
</html>


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单天气预报页面的vue实现。 首先,你需要在你的项目中安装axios和vue-router,以便获取天气数据和进行页面路由。 然后,你需要创建一个组件来显示天气信息。在这个组件中,你可以使用axios来获取天气数据,并将其渲染到页面上。 ```html <template> <div> <h2>天气预报</h2> <div v-if="loading">正在加载中...</div> <div v-if="!loading && weather"> <h3>{{ weather.city }}</h3> <p>{{ weather.weather }}</p> <p>{{ weather.temp }}°C</p> <p>{{ weather.wind }}</p> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { weather: null, loading: true, }; }, async mounted() { const response = await axios.get('http://wthrcdn.etouch.cn/weather_mini?city=北京'); this.weather = response.data.data; this.loading = false; }, }; </script> ``` 在这个组件中,我们使用了一个loading变量来控制当天气数据被加载时显示的文本。我们还使用了async/await来等待axios获取天气数据的响应,然后将响应数据存储在组件的data中,并将loading变量设置为false以显示天气信息。 现在,你可以在你的vue路由器中创建一个路由来显示这个组件。 ```javascript import Vue from 'vue'; import Router from 'vue-router'; import Weather from '@/components/Weather'; Vue.use(Router); export default new Router({ routes: [ { path: '/', name: 'Weather', component: Weather, }, ], }); ``` 最后,你需要在你的应用程序中使用这个路由器,并将其挂载到一个DOM元素上。 ```javascript import Vue from 'vue'; import App from './App'; import router from './router'; Vue.config.productionTip = false; /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>', }); ``` 现在,当你访问你的应用程序时,你将看到一个简单天气预报页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值