微信小程序生成一个天气查询的小程序

微信小程序生成一个天气查询的小程序

基本的页面结构和逻辑

页面结构:包括一个输入框和一个查询按钮。

页面逻辑:在用户输入城市名称后,点击查询按钮,跳转到天气详情页面,并将城市名称作为参数传递。

主要代码

index.js

javascript 复制代码
// index.js
Page({
  data: {
    city: ''
  },
  onInput: function(e) {
    this.setData({
      city: e.detail.value
    });
  },
  onSearch: function() {
    wx.navigateTo({
      url: '/pages/weather?city=' + this.data.city
    });
  }
});

index.wxml

javascript 复制代码
<!-- index.wxml -->
<view class="container">
  <input type="text" placeholder="请输入城市名称" bindinput="onInput"></input>
  <button bindtap="onSearch">查询</button>
</view>

index.wxss

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

天气详情页面(pages/weather)

weather.js

javascript 复制代码
// weather.js
Page({
  data: {
    city: '',
    weather: ''
  },
  onLoad: function(options) {
    const city = options.city;
    this.setData({
      city: city
    });
    // 请求天气数据
    wx.request({
      url: 'https://api.weather.com/v1/current?city=' + city,
      success: res => {
        this.setData({
          weather: res.data.weather
        });
      }
    });
  }
});

weather.wxml

javascript 复制代码
<!-- weather.wxml -->
<view class="container">
  <view class="weather-info">{{ weather }}</view>
</view>

weather.wxss

javascript 复制代码
/* weather.wxss */
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}
.weather-info {
  font-size: 20px;
}

解释

首页和天气详情页。用户可以在首页输入城市名称后,点击查询按钮跳转到天气详情页面,并展示该城市的实时天气信息。

请注意,实际使用中,您需要将请求天气数据的 API 地址和参数进行替换为真实可用的天气数据接口。

到这里也就结束了,希望对您有所帮助。

相关推荐
镭封4 小时前
基于微信小程序的移动端AI配音工作流设计
人工智能·小程序·媒体
FX1317887KP_6 小时前
PET口语备考攻略:发音、语法、表达,三个问题怎么同时解决
小程序·ket口语·pet口语·fce备考·剑桥英语
地球@+jdhb446 小时前
企业私域运营新趋势:快手短视频平台跳转小程序企微卡片链路搭建成为商家运营重点
小程序·企业微信
程序鉴定师7 小时前
2026年深圳小程序/App开发公司技术选型指南:从架构设计到交付标准全维度评估
java·小程序·uni-app·php·objective-c
Q26433650238 小时前
【有源码】基于uni-app的旅游行程规划小程序-基于微信小程序的智慧文旅综合服务平台
java·微信小程序·小程序·uni-app·毕业设计·springboot·源代码
网硕互联的小客服9 小时前
微信小程序多种跳转页面方式教学
微信小程序·小程序·notepad++
m0_587383001 天前
24小时自助健身房软硬件解决方案实战指南:从架构设计到部署实施
java·spring·小程序·架构·需求分析
m0_462803881 天前
会议签到里,手写签名怎样从“额外动作“变成流程环节
微信小程序
m0_587383001 天前
24小时自助健身房软硬件解决方案实战:从架构设计到部署指南
java·spring·小程序·架构·需求分析
CRMEB系统商城1 天前
前后端技术栈全面换代!CRMEB 多商户(Java)v3.0更新预告
java·开发语言·小程序·php