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

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

基本的页面结构和逻辑

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

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

主要代码

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

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

相关推荐
Haibakeji15 小时前
长沙餐饮门店点餐配送小程序定制开发
大数据·小程序
2501_9159184116 小时前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
玩烂小程序17 小时前
微信小程序手串DIY功能开发实录:飞入动画 + 环形排布 + 拖拽换序 + 旋转查看 + 保存设计
微信小程序
程序鉴定师17 小时前
2026济南十大App制作公司测评(精简版):覆盖小程序、定制开发与跨平台方案
大数据·小程序
斯内普吖18 小时前
(开源)高校素拓分管理系统小程序实战指南 基于 Java + SpringBoot + uni-app + Vue + MySQL
java·spring boot·mysql·小程序·uni-app·开源
何时梦醒18 小时前
HTML5 Canvas 从入门到实战:手把手教你打造一款"打飞机"小游戏
微信小程序
master33619 小时前
SSL 证书链问题导致微信小程序无法正常工作
网络协议·微信小程序·ssl
上海观智网络2 天前
上海小程序定制开发合同怎么签?需要注意什么?
经验分享·笔记·小程序
wuxia21182 天前
在5种环境中编写点击元素改变内容和颜色的JavaScript程序
javascript·微信小程序·vue·jquery·react
it-10242 天前
抖音快手短视频去水印微信小程序/一键去水印/小程序去水印接口代码
微信小程序·小程序·php