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

实现天气获取方法

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"
/>

补充天气详情

<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)
  })
}

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

vue实现天气界面

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

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

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

相关文章

vue 实现界面缩放

vue 实现界面缩放

Vue 实现界面缩放的方法 使用 CSS transform 缩放 在 Vue 组件中,可以通过 CSS 的 transform: scale() 属性实现整体缩放效果。创建一个响应式的缩放比例变量,…

vue如何实现界面切换

vue如何实现界面切换

Vue 实现界面切换的方法 使用 Vue Router 实现路由切换 Vue Router 是 Vue.js 的官方路由库,适用于单页面应用(SPA)的界面切换。通过配置路由表,可以实现不同 URL…

vue实现滑动切换界面

vue实现滑动切换界面

Vue 实现滑动切换界面的方法 使用 Vue Touch 或 Hammer.js 库 安装 vue-touch 或 hammer.js 库来处理触摸事件。通过监听滑动手势(如 swipeleft 或…

react实现聊天界面

react实现聊天界面

React 实现聊天界面的核心步骤 搭建基础结构 创建 React 项目后,构建聊天界面的基本框架。通常包括消息列表、输入框和发送按钮。使用 useState 管理消息状态,初始化一个空数组存储消息历…

react实现退出登录界面

react实现退出登录界面

实现退出登录功能 在React中实现退出登录功能通常需要清除用户认证信息并跳转到登录页面。以下是具体实现方法: 清除用户认证状态 使用React的Context或状态管理工具(如Redux)存储用户…

css制作web天气

css制作web天气

使用CSS制作Web天气界面 基础结构设计 HTML部分需要包含天气信息的基本结构,例如城市名称、温度、天气图标和描述。以下是一个简单的HTML结构示例: <div class="weathe…