Vue+axios 获取天气数据

源代码:

<body>
        <div id="app">
            <div class="hello">
                <h2>{{city}}</h2>
                <h4>今天:{{date}} {{week}}</h4>
                <h4>{{message}}</h4>
                <ul>
                    <li v-for="item in obj">
                        <div>
                            <h3>{{item.data}}</h3>
                            <h3>{{item.week}}</h3>
                            <img :src="get(item.wea_img)" alt="">
                            <h3>{{item.wea}}</h3>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
        <script src="../../../js/axios.js"></script>
        <script src="../../../js/vue.global.js"></script>
        <script>
            const vm = Vue.createApp({
                name: 'HelloWorld',
                data() {
                    return {
                        city: "",
                        obj: [],
                        date: "",
                        week: "",
                        message: ""
                    }
                },
                methods: {
                    //定义get方法,拼接图片的路径
                    get(sky) {
                        return "https://xintai.xianguomall.com/skin/pitaya/" + sky + ".png"
                    }
                },
                created() {
                    this.get(); //页面开始加载时调用get方法
                    var that = this;
                    axios.get(

//这里是一个天气网站的api 实况天气接口API开发指南 未来一天天气预报api - 天气API

//点击用户登录,注册一个用户,然后可以获得id和秘钥

//我这里随便打了一个appid,appsecret
                            "https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=123456&appsecret=abcdefg&city=广州")
                        .then(function(response) {
                            //处理数据
                            that.city = response.data.city;
                            that.obj = response.data.data;
                            that.date = response.data.data[0].date;
                            that.week = response.data.data[0].week;
                            that.message = response.data.data[0].air_tips;
                        })
                        .catch(function(error) {
                            console.log(error)
                        })
                }
            }).mount("#app");
        </script>
        <style scoped>
            h2,
            h4 {
                text-align: center;
            }

            li {
                float: left;
                list-style-type: none;
                width: 200px;
                text-align: center;
                border: 1px solid red;
            }
        </style>
        </div>
    </body>

最后效果是这样的

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值