Vue实现天气预报功能

2022-04-15 0 449

本文为大家分享了Vue实现天气预报功能的具体代码,供大家参考,具体内容如下

1、功能描述

在搜索框中输入城市,下方出现今天及未来四天的天气情况。搜索框下面固定了几个城市,点击可以快速查询。

2、html代码

 <div id="app">
        <div id="srchbar">
            <input type="text" v-model="city" @keyup.enter="srch(city)" id="ipt">
            <a @click=srch(city) id="btn">search</a>
        </div>
        <nav>
            <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="srch('北京')">北京</a>
            <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="srch('上海')">上海</a>
            <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="srch('广州')">广州</a>
            <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="srch('深圳')">深圳</a>
        </nav>
        <div id="res">
            <table>
                <tr>
                    <th v-for="item in forecasts">{{item.type}}</th>
                </tr>
                <tr>
                    <td v-for="item in forecasts">{{item.low}}~{{item.high}}</td>
                </tr>
                <tr>
                    <td v-for="item in forecasts">{{item.date}}</td>
                </tr>
            </table>
        </div>
</div>

3、js代码

var app = new Vue({
        el: "#app",
        data: {
            city: "",
            forecasts: []
        },
        methods: {
            srch: function (c) {
                var that = this;
                axios.get("http://wthrcdn.etouch.cn/weather_mini?city=" + c).then(function (message) {
                    that.city = c;
                    that.forecasts = message.data.data.forecast;
                })
            }

        }
})

结果示意

Vue实现天气预报功能

总结

主要练习了v-for, v-model, v-on表达式,以及使用axios通过接口请求数据。

小编之前学习过程中曾将收藏了一段关于天气预报功能的js关键代码,分享给大家,一起学习。

// 请求地址:http://wthrcdn.etouch.cn/weather_mini
// 请求方法:get,
// 请求参数:city(城市名)
// 响应内容:天气信息,

// 1.点击回车
// 2.查询数据
// 3.渲染数据

var app = new Vue({
 el: '#app',
 data: {
  city: '',
  weatherList: [],
 },
 methods: {
  serchWeather: function() {
   // console.log('天气查询');
   // console.log(this.city)
   // 调用接口
   //保存this
   var that = this;
   axios.get('http://wthrcdn.etouch.cn/weather_mini?city=' + this.city)
    .then(function(response) {
     console.log(response.data.data.forecast)
     that.weatherList = response.data.data.forecast
    }).catch(function(err) {})
  },
  changeCity: function(city) {
          //1.改城市
     //2.查天气
     this.city=city;
     this.serchWeather();
  }
 }
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript Vue实现天气预报功能 https://www.niceym.com/23236.html