【vue3+和风天气插件】城市实时天气

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

【vue3+和风天气插件】城市实时天气


前言

使用vue3+和风插件,可以轻松实现自定义化城市实时天气小组件,再也不用手撕代码了!
保姆级教程,安心使用:


一、前提准备

1.登录和风插件开发平台
2.自定义组件
在这里插入图片描述
3.点击生成代码
在这里插入图片描述

二、vue3中使用

1.在自己的组件中使用

注意:不能完全复制和风的代码,需要自己改下。下面是我自己写好的模板,可直接套用 ↓
代码如下(示例):

<template>
  <div class="box">
    <div id="he-plugin-standard"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  created() {
    window.WIDGET = {
      CONFIG: {
        layout: "1",
        width: 450,
        height: 150,
        background: "1",
        dataColor: "FFFFFF",
        borderRadius: "5",
        key: "04c1df5a350e448cb2b2728f1b435f88",
      },
    };
    let script = document.createElement("script");
    script.src =
      "https://widget.qweather.net/standard/static/js/he-standard-common.js?v=2.0";
    document.getElementsByTagName("head")[0].appendChild(script);
  },
  methods: {},
};
</script>

<style  scoped>
</style>

2.展示效果

在这里插入图片描述

3.更改可视化效果

在和风天气开发平台上改变样式,以后把更新的代码的这部分替换掉即可。
在这里插入图片描述在这里插入图片描述

总结

在做项目时使用插件真的很方便,操作难度系数很低!

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
好的,我可以为您提供一个基于和天气API的Vue天气预报示例。 首先,您需要在和天气官网注册并创建一个应用,以获取访问API的key。 然后,您可以创建一个Vue项目,并安装axios和vue-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来获取天气预报了。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值