欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實(shí)現(xiàn)天氣預(yù)報(bào)功能

 更新時(shí)間:2021年08月26日 16:06:00   作者:DaftWeeknd  
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)天氣預(yù)報(bào)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了Vue實(shí)現(xiàn)天氣預(yù)報(bào)功能的具體代碼,供大家參考,具體內(nèi)容如下

1、功能描述

在搜索框中輸入城市,下方出現(xiàn)今天及未來(lái)四天的天氣情況。搜索框下面固定了幾個(gè)城市,點(diǎn)擊可以快速查詢。

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="#" @click="srch('北京')">北京</a>
            <a href="#" @click="srch('上海')">上海</a>
            <a href="#" @click="srch('廣州')">廣州</a>
            <a href="#" @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;
                })
            }

        }
})

結(jié)果示意

總結(jié)

主要練習(xí)了v-for, v-model, v-on表達(dá)式,以及使用axios通過(guò)接口請(qǐng)求數(shù)據(jù)。

小編之前學(xué)習(xí)過(guò)程中曾將收藏了一段關(guān)于天氣預(yù)報(bào)功能的js關(guān)鍵代碼,分享給大家,一起學(xué)習(xí)。

// 請(qǐng)求地址:http://wthrcdn.etouch.cn/weather_mini
// 請(qǐng)求方法:get,
// 請(qǐng)求參數(shù):city(城市名)
// 響應(yīng)內(nèi)容:天氣信息,

// 1.點(diǎn)擊回車
// 2.查詢數(shù)據(jù)
// 3.渲染數(shù)據(jù)

var app = new Vue({
 el: '#app',
 data: {
  city: '',
  weatherList: [],
 },
 methods: {
  serchWeather: function() {
   // console.log('天氣查詢');
   // console.log(this.city)
   // 調(diào)用接口
   //保存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();
  }
 }
})

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論