当前位置:首页 > 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

安装axios发送请求

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

npm install axios

创建天气查询组件

新建Weather.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 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

vue 实现工具

vue 实现工具

Vue 实现工具的方法 使用 Vue 实现工具功能可以通过多种方式完成,具体取决于工具的类型和需求。以下是几种常见的方法: 组件化实现 创建一个独立的 Vue 组件来封装工具功能。例如,实现一…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transit…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="co…