龙空技术网

vue——fetch请求天气接口,展示数据

北漂佳佳的生活 87

前言:

现时兄弟们对“js获取天气预报接口”可能比较关心,你们都想要了解一些“js获取天气预报接口”的相关内容。那么小编也在网摘上网罗了一些有关“js获取天气预报接口””的相关资讯,希望小伙伴们能喜欢,小伙伴们快快来了解一下吧!

<script src=";></script><div id="app">        <form>            <input type="text" v-model="city" >            <input type="submit" value="提交" @click="searchWeather">        </form>        <div id="weather">           <ul>               <li v-for="(tmp,index) in tmps">                   <p>地区:{{tmp.chengshi}}---温度:{{tmp.tmp}}---天气状况描述:{{tmp.cond_txt}}</p>                   <p></p>                </li>           </ul>        </div><script>        var app=new Vue({            el:"#app",            data:{                city:'',                tmps:[],                chengshi:'',                           },           methods: {            searchWeather:async function(e){   //异步                e.preventDefault();   //阻止form表单默认提交行为                let httpUrl=`{this.city}&key=b888d12c8d224f678c9b4955087a1ae3`;//和风天气的api                let res=await fetch(httpUrl); //                let result =await res.json();                console.log(result);//打印一下,便于了解数据情况                let now = result.HeWeather6[0].now;//得到天气情况的object                let chengshi='chengshi'; //设置地区的键key                let location=result.HeWeather6[0].basic.location;//获取地区的值value                now[chengshi]=location;//追加一组地区的数据                this.tmps.push(now);//追加到数组中,遍历所填写的数据                                    this.city='';//每次输入后,input为空            }           },        })    </script>

标签: #js获取天气预报接口