vue和axios做的查询天气

利用axios调用接口,vue获取和绑定数据

Enter事件查询,还有热键查询

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

</head>

<body>
    <div id="app" class="wrap">
        <div class="search_form">
            <div class="logo"><img src="../vue/img/01.png" alt="logo" style="width: 200px;"></div>
            <div class="form_group">
                <input type="text" @keyup.enter='searchWeather' v-model="city" class="input_txt"
                    placeholder="请输入从查询的城市">
                <button class="input_sub">搜 索</button>
            </div>
            <div class="hotkey">
                <a href="javascript:;" @click='changeCity("北京")'>北京</a>
                <a href="javascript:;" @click='changeCity("上海")'>上海</a>
                <a href="javascript:;" @click='changeCity("广州")'>广州</a>
                <a href="javascript:;" @click='changeCity("深圳")'>深圳</a>
            </div>
            <ul class="weather_list">
                <li v-for="(item, index) in weatherList" :key="index">
                    <div class="info_type"><span class="iconfont">{{item.type}}</span></div>
                    <div class="info_temp">
                        <b>{{item.low}}</b>
                        ~
                        <b>{{item.high}}</b>
                    </div>
                    <div class="info_date">
                        <span>{{item.date}}</span>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <script>
        /*
        天气接口
        请求地址:http://wthrcdn.etouch.cn/weather_mini
        请求方法:get
        请求参数:city
        响应内容:天气信息
        */
        // 1.点击回车
        // 2.查询数据
        // 3.渲染数据
        const app = new Vue({
            el: '#app',
            data: {
                city: '',
                weatherList: [],
            },
            methods: {
                searchWeather: function (city) {
                    const that = this;
                    // console.log('weather');
                    // 调用接口
                    axios.get('http://wthrcdn.etouch.cn/weather_mini?city=' + this.city)
                        .then(function (response) {
                            // console.log(response);
                            that.weatherList = response.data.data.forecast;
                            // console.log(that.weatherList);
                            
                        })
                        .catch(function (error) {
                            console.log(error);

                        })

                },
                changeCity:function(city){
                    this.city = city;
                    this.searchWeather();
                }
            }
        })
    </script>
</body>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的天气预报页面的vue实现。 首先,你需要在你的项目中安装axiosvue-router,以便获取天气数据和进行页面路由。 然后,你需要创建一个组件来显示天气信息。在这个组件中,你可以使用axios获取天气数据,并将其渲染到页面上。 ```html <template> <div> <h2>天气预报</h2> <div v-if="loading">正在加载中...</div> <div v-if="!loading && weather"> <h3>{{ weather.city }}</h3> <p>{{ weather.weather }}</p> <p>{{ weather.temp }}°C</p> <p>{{ weather.wind }}</p> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { weather: null, loading: true, }; }, async mounted() { const response = await axios.get('http://wthrcdn.etouch.cn/weather_mini?city=北京'); this.weather = response.data.data; this.loading = false; }, }; </script> ``` 在这个组件中,我们使用了一个loading变量来控制当天气数据被加载时显示的文本。我们还使用了async/await来等待axios获取天气数据的响应,然后将响应数据存储在组件的data中,并将loading变量设置为false以显示天气信息。 现在,你可以在你的vue路由器中创建一个路由来显示这个组件。 ```javascript import Vue from 'vue'; import Router from 'vue-router'; import Weather from '@/components/Weather'; Vue.use(Router); export default new Router({ routes: [ { path: '/', name: 'Weather', component: Weather, }, ], }); ``` 最后,你需要在你的应用程序中使用这个路由器,并将其挂载到一个DOM元素上。 ```javascript import Vue from 'vue'; import App from './App'; import router from './router'; Vue.config.productionTip = false; /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>', }); ``` 现在,当你访问你的应用程序时,你将看到一个简单的天气预报页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值