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

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

基本的页面结构和逻辑

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

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

主要代码

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

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

相关推荐
mon_star°40 分钟前
消防安全培训小程序项目亮点与功能清单
小程序
自然 醒1 小时前
uni-app开发微信小程序,如何使用towxml去渲染md格式和html标签格式的内容?
微信小程序·uni-app·html
编程迪1 小时前
基于Java和Vue开发的在线问诊系统医疗咨询小程序APP
小程序
CHU7290353 小时前
知识触手可及:在线教学课堂APP的沉浸式学习体验
前端·学习·小程序
竟未曾年少轻狂3 小时前
微信小程序-组件开发
微信小程序·小程序
CHU7290353 小时前
在线教学课堂APP功能版块设计方案:重构学习场景的交互逻辑
java·学习·小程序·重构
焦糖玛奇朵婷4 小时前
盲盒小程序开发,盲盒小程序怎么做
java·大数据·服务器·前端·小程序
想七想八不如114084 小时前
【GitHub开源】一款极简跨平台 Todo 应用:微信小程序 + Windows 桌面挂件 + 实时同步
微信小程序·开源·github
笨笨狗吞噬者5 小时前
代理的妙用:uni-app 小程序是怎样用 `Proxy` 和 `wrapper` 抹平平台差异的
前端·微信小程序·uni-app
CHU7290351 天前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序