利用vue+axios实现简单天气查询

一、代码:

<!DOCTYPE html>
<html>
<head>
    <title>天气查询案例</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    </head>
<body>
    <div class="container mt-5">
        
        <div id="app" class="d-flex flex-column">
            <div class="d-flex justify-content-center">
                <h3 class="d-inline">天气查询</h3>
            </div> 
            <div class="d-flex justify-content-center mt-4">
                <input type="text" class="form-control col-5" placeholder="输入城市名称然后回车..." v-model="city"  @keyup.enter="search">
                <button class="btn btn-primary mb-4 col-1" @click="search"> 搜索</button>
                <br>
                
            </div>

            <!-- 选择城市 -->
            <div class="d-flex  justify-content-center">
                <button class="btn btn-primary btn-sm " @click="changeCity('北京')">北京</button> 
                <button class="btn btn-primary btn-sm mx-5" @click="changeCity('上海')">上海</button>
                <button class="btn btn-primary btn-sm " @click="changeCity('天津')">天津</button>
                <button class="btn btn-primary btn-sm mx-5" @click="changeCity('重庆')">重庆</button>
            </div>
            
            <div >
                <span v-show="isActive">地点:{{fl}}</span>
                <div class="card card-body " v-for="weather in list">
                    <span>日期:{{weather.day}}</span>
                    <span>天气:{{weather.wea}}</span>
                </div>
                

            </div>
        </div>
    </div>
</body>
<!-- 官网提供的 axios 在线地址 -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<script>
    // 我自己的script
    var app = new Vue({
        el:'#app',
        data:{
            isActive:false,
            city:"",
            fl:'',
            list:[]
        },
        methods:{
            search(){
                var that = this;
                // alert(that.weatherList)
                axios.get("http://ajax-api.itheima.net/api/weather?city=" + that.city).then
                (function(response){
                  that.isActive = true;
                  that.list =  response.data.data.data;
                  that.fl=  response.data.data.city

                  console.log(that.fl);
                    console.log(that.list);
                },function (err){})
            },
            changeCity(c1){
                this.city = c1;
                this.search();
            }
        }
    })

</script>


</html>

二、效果展示

①点击按钮直接查询到天气。

在这里插入图片描述

②输入完毕后摁下”enter“,或点击搜索查询天气

在这里插入图片描述

三、心得

1.城市的选择。所有的备选城市后面的点击事件都传入一个参数,到时候直接使用这个参数就可以。

<div class="d-flex  justify-content-center">
                <button class="btn btn-primary btn-sm " @click="changeCity('北京')">北京</button> 
                <button class="btn btn-primary btn-sm mx-5" @click="changeCity('上海')">上海</button>
                <button class="btn btn-primary btn-sm " @click="changeCity('天津')">天津</button>
                <button class="btn btn-primary btn-sm mx-5" @click="changeCity('重庆')">重庆</button>
 </div>
  1. 点击城市后直接出现天气详情。在方法里可以直接调用其他函数。

在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个基于和风天气API的Vue天气预报示例。 首先,您需要在和风天气官网注册并创建一个应用,以获取访问API的key。 然后,您可以创建一个Vue项目,并安装axiosvue-axios插件来进行API请求。 接下来,您可以在Vue组件中编写代码来获取天气数据并渲染到页面上。以下是示例代码: ```html <template> <div class="weather"> <h1>{{ city }}天气预报</h1> <div v-if="loading">加载中...</div> <div v-else> <div>当前温度:{{ temperature }}℃</div> <div>天气状况:{{ weather }}</div> <div>风向:{{ windDirection }}</div> <div>风力:{{ windLevel }}</div> <div>湿度:{{ humidity }}%</div> </div> </div> </template> <script> import axios from 'axios'; import VueAxios from 'vue-axios'; export default { data() { return { city: '北京', loading: true, temperature: null, weather: null, windDirection: null, windLevel: null, humidity: null } }, mounted() { this.getWeather(); }, methods: { getWeather() { const key = 'YOUR_API_KEY'; const url = `https://free-api.heweather.net/s6/weather/now?location=${this.city}&key=${key}`; this.$http.get(url).then(response => { const data = response.data.HeWeather6[0].now; this.temperature = data.tmp; this.weather = data.cond_txt; this.windDirection = data.wind_dir; this.windLevel = data.wind_sc; this.humidity = data.hum; this.loading = false; }).catch(error => { console.log(error); }); } }, plugins: [ VueAxios, axios ] } </script> <style> .weather { text-align: center; } </style> ``` 在上面的代码中,您需要将`YOUR_API_KEY`替换为您的和风天气API key,并将`city`设置为您想要获取天气的城市名称。 最后,您可以在Vue根实例中引入该组件并将其渲染到页面上。 ```html <template> <div id="app"> <weather></weather> </div> </template> <script> import Weather from './components/Weather.vue'; export default { components: { Weather } } </script> ``` 这样,您就可以在Vue中使用和风天气API来获取天气预报了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值