当前位置:首页 > VUE

vue实现天气查询

2026-02-11 08:48:24VUE

实现天气查询功能

使用Vue实现天气查询功能需要调用天气API获取数据,并将结果展示在页面上。以下是具体实现方法:

注册天气API服务

推荐使用高德地图天气API、和风天气API或OpenWeatherMap等免费服务。注册账号后获取API密钥,用于后续请求授权。

例如高德天气API的注册地址为:https://lbs.amap.com/api/javascript-api/guide/services/weather

vue实现天气查询

安装axios发送请求

在Vue项目中安装axios库处理HTTP请求:

npm install axios

创建天气查询组件

新建Weather.vue组件文件,包含查询输入框和结果显示区域:

vue实现天气查询

<template>
  <div>
    <input v-model="city" placeholder="输入城市名称"/>
    <button @click="getWeather">查询</button>
    <div v-if="weatherData">
      <h3>{{ weatherData.city }}天气</h3>
      <p>温度:{{ weatherData.temperature }}℃</p>
      <p>天气:{{ weatherData.weather }}</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      city: '',
      weatherData: null
    }
  },
  methods: {
    async getWeather() {
      try {
        const response = await axios.get(
          `https://restapi.amap.com/v3/weather/weatherInfo?key=你的API密钥&city=${this.city}`
        );
        this.weatherData = {
          city: response.data.lives[0].city,
          temperature: response.data.lives[0].temperature,
          weather: response.data.lives[0].weather
        };
      } catch (error) {
        console.error('获取天气失败:', error);
      }
    }
  }
}
</script>

处理API响应数据

根据不同的天气API返回格式,调整数据处理逻辑。高德API返回的天气数据格式为:

{
  "lives": [
    {
      "city": "北京市",
      "weather": "晴",
      "temperature": "23"
    }
  ]
}

添加样式美化界面

为组件添加CSS样式提升用户体验:

<style scoped>
input {
  padding: 8px;
  margin-right: 10px;
}
button {
  padding: 8px 16px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
}
</style>

错误处理和加载状态

添加加载状态和错误提示提升交互体验:

<template>
  <div>
    <input v-model="city" placeholder="输入城市名称"/>
    <button @click="getWeather" :disabled="loading">
      {{ loading ? '查询中...' : '查询' }}
    </button>
    <div v-if="error" class="error">{{ error }}</div>
    <div v-if="weatherData">
      <!-- 天气信息展示 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      error: ''
    }
  },
  methods: {
    async getWeather() {
      this.loading = true;
      this.error = '';
      try {
        // 请求代码
      } catch (error) {
        this.error = '获取天气信息失败,请重试';
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style>
.error {
  color: red;
  margin-top: 10px;
}
</style>

部署注意事项

  1. 将API密钥存储在环境变量中而非代码里
  2. 考虑添加防抖功能减少API请求次数
  3. 对于生产环境,建议通过后端服务代理天气API请求

以上实现方案可以扩展添加更多天气细节如湿度、风速等,或增加天气预报功能显示未来几天天气情况。

标签: 天气vue
分享给朋友:

相关文章

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue怎么实现文件树

vue怎么实现文件树

Vue 实现文件树的方法 使用递归组件 递归组件是处理嵌套数据结构的理想方式。文件树通常包含文件夹和文件的嵌套结构,适合用递归组件实现。 <template> <ul>…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…