Vue實現(xiàn)天氣預(yù)報功能
本文為大家分享了Vue實現(xiàn)天氣預(yù)報功能的具體代碼,供大家參考,具體內(nèi)容如下
1、功能描述
在搜索框中輸入城市,下方出現(xià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表達式,以及使用axios通過接口請求數(shù)據(jù)。
小編之前學(xué)習(xí)過程中曾將收藏了一段關(guān)于天氣預(yù)報功能的js關(guān)鍵代碼,分享給大家,一起學(xué)習(xí)。
// 請求地址:http://wthrcdn.etouch.cn/weather_mini // 請求方法:get, // 請求參數(shù):city(城市名) // 響應(yīng)內(nèi)容:天氣信息, // 1.點擊回車 // 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(); } } })
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue利用全局導(dǎo)航守衛(wèi)作登錄后跳轉(zhuǎn)到未登錄前指定頁面的實例代碼
這篇文章主要介紹了vue利用全局導(dǎo)航守衛(wèi)作登錄后跳轉(zhuǎn)到未登錄前指定頁面,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05