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

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

  • 第一步创建一个项目
  • [第二步项目目录下找到 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}%`
          }
        });
      }
    });
  }
});

项目简介

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

相关推荐
fakaifa5 小时前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_9159184112 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售17 小时前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
suncentwl18 小时前
做一个答题pk小程序多少钱?
小程序·答题小程序·知识竞赛·答题pk软件
说私域19 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化策略研究
人工智能·小程序
咸虾米_19 小时前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
微信小程序·小程序·uniapp开发·小程序地图api
未来之窗软件服务20 小时前
蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
小程序·自动化·仙盟创梦ide·东方仙盟·蔬菜批发·批发系统
数据皮皮侠1 天前
最新上市公司业绩说明会文本数据(2017.02-2025.08)
大数据·数据库·人工智能·笔记·物联网·小程序·区块链
不如摸鱼去2 天前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程
意会2 天前
微信闪照小程序实现
前端·css·微信小程序