当前位置:首页 > 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定义不同天气的背景样式。

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

相关文章

css制作登录界面

css制作登录界面

使用CSS制作登录界面 登录界面是网站常见的功能模块,通过CSS可以实现美观且响应式的设计。以下是一个基础的登录界面实现方法。 HTML结构 基础的HTML结构包含表单元素,如输入框、按钮和标签。…

vue实现界面跳转

vue实现界面跳转

Vue 实现界面跳转的方法 在 Vue 中实现界面跳转通常使用 Vue Router,以下是几种常见的实现方式: 使用 router-link 组件 通过声明式导航实现跳转,适合模板中使用:…

vue界面实现滚动

vue界面实现滚动

实现滚动效果的方法 在Vue中实现滚动效果可以通过多种方式,包括原生JavaScript、CSS或第三方库。以下是几种常见的方法: 使用CSS实现滚动 通过CSS的overflow属性可以轻松…

uniapp 界面模板

uniapp 界面模板

Uniapp 界面模板推荐 Uniapp 提供了丰富的界面模板和组件库,开发者可以直接使用或基于这些模板进行二次开发。以下是一些常用的界面模板和资源: 官方模板与示例 Uniapp 官方提供了多个示…

vue实现后台界面

vue实现后台界面

Vue 实现后台界面的方法 使用 Vue 实现后台界面通常需要结合 UI 框架、路由管理和状态管理工具。以下是具体实现步骤: 1. 项目初始化 使用 Vue CLI 或 Vite 创建项目: np…

css制作登录界面

css制作登录界面

使用CSS制作登录界面 登录界面是网站常见的功能模块,通过CSS可以轻松实现美观且响应式的设计。以下是实现登录界面的关键步骤和代码示例。 HTML结构 基础的HTML结构包含表单、输入框和按钮等元…