微信小程序实现一个天气预报应用程序

微信小程序实现一个天气预报应用程序

  • 第一步创建一个项目
  • [第二步项目目录下找到 pages/index/index.wxml 文件](#第二步项目目录下找到 pages/index/index.wxml 文件)
  • [第三步在 pages/index/index.wxss 文件中写入样式](#第三步在 pages/index/index.wxss 文件中写入样式)
  • [第四步在 pages/index/index.js 文件中添加以下代码](#第四步在 pages/index/index.js 文件中添加以下代码)
  • 项目简介

第一步创建一个项目

第二步项目目录下找到 pages/index/index.wxml 文件

javascript 复制代码
<view class="container">
  <view class="header">
    <input class="input" placeholder="请输入城市名称" bindinput="onInput" value="{{ inputValue }}" />
    <button class="button" bindtap="searchWeather">查询</button>
  </view>
  <view class="weather-info" wx:if="{{ weatherData }}">
    <view class="city">{{ weatherData.city }}</view>
    <view class="temperature">温度:{{ weatherData.temperature }}℃</view>
    <view class="weather">{{ weatherData.weather }}</view>
    <view class="wind">风向:{{ weatherData.wind }}</view>
    <view class="humidity">湿度:{{ weatherData.humidity }}</view>
  </view>
</view>

第三步在 pages/index/index.wxss 文件中写入样式

javascript 复制代码
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.input {
  flex-grow: 1;
  padding: 10px;
  border: 1px solid #CCC;
  border-radius: 4px;
}

.button {
  padding: 10px 20px;
  margin-left: 10px;
  background-color: #007AFF;
  color: #FFF;
  border-radius: 4px;
}

.weather-info {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.city {
  font-size: 24px;
  margin-bottom: 10px;
}

.temperature {
  font-size: 16px;
  margin-bottom: 10px;
}

.weather {
  font-size: 16px;
  margin-bottom: 10px;
}

.wind {
  font-size: 16px;
  margin-bottom: 10px;
}

.humidity {
  font-size: 16px;
  margin-bottom: 10px;
}

第四步在 pages/index/index.js 文件中添加以下代码

javascript 复制代码
Page({
  data: {
    inputValue: '', // 输入框的值
    weatherData: null // 天气数据
  },
  onInput(e) {
    this.setData({
      inputValue: e.detail.value
    });
  },
  searchWeather() {
    const that = this;
    wx.request({
      url: 'https://api.weatherapi.com/v1/current.json',
      data: {
        key: 'YOUR_API_KEY',
        q: this.data.inputValue,
        aqi: 'no'
      },
      success(res) {
        const weather = res.data.current;
        that.setData({
          weatherData: {
            city: res.data.location.name,
            temperature: weather.temp_c,
            weather: weather.condition.text,
            wind: `${weather.wind_dir} ${weather.wind_kph}km/h`,
            humidity: `${weather.humidity}%`
          }
        });
      }
    });
  }
});

项目简介

这个示例展示了一个天气预报应用程序,包括一个输入框和一个查询按钮,用于查询指定城市的实时天气信息。在下方显示了城市名称、温度、天气状况、风向和湿度。

相关推荐
00后程序员张8 小时前
苹果App Store上架指南:费用、原理与步骤详解
android·ios·小程序·https·uni-app·iphone·webview
paopaokaka_luck17 小时前
个人综合管理小程序(echarts数据看板、健康数据记录与趋势查看、目标拆解和进度管理、日程待办与番茄钟、习惯打卡、收支统计、个人生活记录、跨模块智能提醒)
前端·javascript·spring·小程序·数据分析·echarts·生活
愚公搬代码17 小时前
【愚公系列】《微信小程序项目实战(AI编程+视频图解)》002-一张新地图:AI创业的核心分析框架
人工智能·微信小程序·ai编程
m0_587383001 天前
广州24小时自助健身房解决方案实战指南与系统部署要点
java·spring·小程序·架构·需求分析
ZeroNews内网穿透1 天前
企业内网穿透别再用随机域名!自有域名实现 OA/ERP 公网安全访问
微信小程序·内网穿透·erp·zeronews·运维实战·企业oa·自有域名
m0_587383001 天前
西安同城拼车软件开发实战指南:从零搭建高效系统
人工智能·小程序·数据挖掘·系统架构·需求分析
weixin_lynhgworld1 天前
抽取类盲盒小程序的五条合规红线意味着什么?
小程序
现任明教教主~1 天前
运营级多门店洗车贴膜汽车在线核销保养服务系统洗车预约洗车系统消费分佣扫码核销系统
微信小程序·小程序·汽车
小小王app小程序开发1 天前
盲盒微信小程序开发实战:架构、核心难点与实现方案
小程序
陪我去看海2 天前
被要求猛出小程序,做了这个多平台多环境的部署替我承受压力
前端·微信小程序·抖音小程序