当前位置:首页 > 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 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &l…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncComp…

vue实现一个tab栏

vue实现一个tab栏

Vue 实现 Tab 栏 基础实现(选项式 API) <template> <div class="tab-container"> <div class="t…

vue 实现直播

vue 实现直播

Vue 实现直播的基本方法 在Vue中实现直播功能,通常需要结合WebRTC、RTMP或HLS等技术。以下是几种常见的实现方式: 使用WebRTC实现实时直播 WebRTC适合低延迟的实时直播场景,…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…