当前位置:首页 > VUE

vue实现天气界面

2026-01-16 06:54:29VUE

实现天气界面的核心步骤

安装依赖与项目初始化 使用Vue CLI或Vite创建项目,安装axios用于API请求。如使用npm:npm install axios

获取天气API密钥 注册开放天气API服务(如OpenWeatherMap),获取免费API密钥。注意免费版可能有调用频率限制。

创建基础组件结构

<template>
  <div class="weather-app">
    <div class="search-box">
      <input 
        type="text" 
        v-model="city" 
        @keyup.enter="fetchWeather"
        placeholder="输入城市名称"
      />
    </div>
    <div class="weather-wrap" v-if="weatherData">
      <!-- 天气信息展示区 -->
    </div>
  </div>
</template>

数据获取与处理

设置响应式数据

data() {
  return {
    city: '',
    weatherData: null,
    apiKey: 'YOUR_API_KEY',
    error: null
  }
}

实现天气获取方法

vue实现天气界面

methods: {
  async fetchWeather() {
    try {
      const response = await axios.get(
        `https://api.openweathermap.org/data/2.5/weather?q=${this.city}&units=metric&appid=${this.apiKey}`
      )
      this.weatherData = response.data
      this.error = null
    } catch (err) {
      this.error = '城市未找到,请重试'
      this.weatherData = null
    }
  }
}

界面展示优化

主要天气信息展示

<div class="weather-box">
  <div class="temp">{{ Math.round(weatherData.main.temp) }}°C</div>
  <div class="weather">{{ weatherData.weather[0].description }}</div>
</div>

添加天气图标 使用API返回的图标代码动态显示:

<img 
  :src="`https://openweathermap.org/img/wn/${weatherData.weather[0].icon}@2x.png`" 
  alt="Weather icon"
/>

补充天气详情

vue实现天气界面

<div class="details">
  <div class="humidity">
    <span>湿度</span>
    {{ weatherData.main.humidity }}%
  </div>
  <div class="wind">
    <span>风速</span>
    {{ weatherData.wind.speed }} km/h
  </div>
</div>

样式设计与交互增强

基础样式示例

.weather-app {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
}

.search-box input {
  width: 100%;
  padding: 10px;
  font-size: 16px;
}

.weather-wrap {
  margin-top: 20px;
  background: rgba(255,255,255,0.2);
  padding: 20px;
  border-radius: 10px;
}

.temp {
  font-size: 4rem;
  font-weight: bold;
}

添加加载状态 在data中添加loading状态,请求时设为true,完成后设为false,配合v-if显示加载动画。

位置自动检测 可使用浏览器Geolocation API获取用户位置,然后反向查询城市名称:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(position => {
    this.fetchWeatherByCoords(position.coords.latitude, position.coords.longitude)
  })
}

实现天气背景变化 根据天气状况动态改变背景:

computed: {
  weatherClass() {
    if (!this.weatherData) return ''
    const main = this.weatherData.weather[0].main.toLowerCase()
    return `weather-${main}`
  }
}

对应CSS定义不同天气的背景样式。

标签: 界面天气
分享给朋友:

相关文章

vue界面实现mqtt

vue界面实现mqtt

实现Vue界面集成MQTT通信 安装MQTT客户端库 在Vue项目中安装mqtt库,通过npm或yarn执行安装命令: npm install mqtt --save # 或 yarn add m…

vue实现登录界面

vue实现登录界面

创建Vue项目 使用Vue CLI创建一个新项目,确保已安装Node.js和Vue CLI。 vue create login-app 进入项目目录并安装必要的依赖。 cd login-app n…

用vue实现注册界面

用vue实现注册界面

使用Vue实现注册界面 创建Vue项目 使用Vue CLI创建一个新项目: vue create register-app cd register-app 安装必要依赖 安装Vue Router和…

react中如何引用一个界面

react中如何引用一个界面

在 React 中引用一个界面(组件) React 的核心思想是基于组件的模块化开发,引用一个界面通常意味着引用另一个组件。以下是几种常见的方法: 导入组件文件 假设有一个名为 MyComponen…

react实现注册界面

react实现注册界面

以下是一个基于React的注册界面实现方案,包含关键代码和功能说明: 基础组件结构 使用函数组件搭配React Hooks管理状态: import React, { useState }…

react实现退出界面

react实现退出界面

实现退出界面的方法 在React中实现退出界面通常涉及路由跳转或状态管理。以下是几种常见的方法: 使用React Router进行导航 安装React Router依赖: npm install…