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

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

基本的页面结构和逻辑

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

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

主要代码

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 地址和参数进行替换为真实可用的天气数据接口。

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

相关推荐
FX1317887KP_10 小时前
KET/PET/FCE 口语答案说多长才合适?长度参考与展开方法
小程序·ket口语·pet口语·fce备考·剑桥英语
律宏阔13 小时前
微信小程序 + Taro + TanStack Query 缓存与页面返回刷新踩坑实录
前端·微信小程序·taro
千寻技术帮14 小时前
微信小程序+Spring Boot 烘焙商城系统(附完整源码+数据库+部署文档)50053
数据库·spring boot·微信小程序·烘焙商城
码云之上1 天前
微信小程序设备指纹实践,删了重装还能认出这台手机吗?
前端·安全·微信小程序
源码手记2 天前
基于微信小程序的相亲交友智能匹配系统-附源码
微信小程序·小程序·交友
源码手记2 天前
基于微信小程序的图书馆座位预约系统(程序+文档)
微信小程序·小程序
aimartec2 天前
企业小程序与APP定制源码买断工程实践:三层解耦架构、状态机防呆与平滑迁移清单
小程序·架构
源码手记2 天前
基于微信小程序的校园失物招领系统-附源码
微信小程序·小程序
源码手记2 天前
基于微信小程序的体育场馆预约系统-源码文档
微信小程序·小程序
源码手记2 天前
基于微信小程序的物业管理系统-附源码
微信小程序·小程序