当前位置: 首页 > news >正文

实时更新天气微信小程序开发

1.新建一个天气weather项目

2.在app.json中创建一个路由页面

 当我们点击保存的时候,微信小程序会自动的帮我们创建好页面

3.在weather页面上书写我们的骨架

 4.此时我们的页面很怪,因为没有给它添加样式和值。此时我们给它一个样式。(样式写在wxss中)

5.给它值,使用插值表达式,数据放在js的data中

 6.完善代码,用wx-for的方式渲染出今天,明天,昨天的天气情况。温馨提示

 7.完整代码

weather.wxml

<!--pages/weather/weather.wxml-->
<view class="cotent"><view class="today"><view class="info"><view class="temp">{{temp}}℃</view><view class="lowhigh">{{low}}/{{high}}</view><view class="type">{{type}}</view><view class="city">{{city}}</view><view class="week">{{week}}</view><view class="weather">{{weather}}</view></view></view><view class="viewLine"></view><view class="otherWeather"><view class="weatherId" wx:for="{{otherWeather}}" wx:key="index"><view class="infoOther"><view class="typeOther">{{item.type}}</view><view class="lowhighOther">{{item.low}}/{{item.high}}</view><view class="weekOther">{{item.week}}</view><view class="weatherOther">{{item.direction}}</view><view class="weatherOther">{{item.windLeve}}</view></view></view></view><view class="tip">友情提示:天气变凉,空气湿度较大,易发生感冒,请注意适当增加衣物,加强自我防护,避免感冒。</view>
</view>

 weather.js

// pages/weather/weather.js
Page({/*** 页面的初始数据*/data: {temp: "4",low: "-1℃",high: "10℃",type: "晴",city: "北京",week: "八日星期二",weather: "无持续风向 微风级",otherWeather: [{id: "002",low: "2℃",high: "10℃",type: "多云",week: "9日星期三",direction:"无持续风向",windLeve: "向微风级",},{id: "003",low: "0℃",high: "9℃",type: "多云",week: "10日星期四",direction:"无持续风向",windLeve: "向微风级",},{id: "004",low: "-1℃",high: "10℃",type: "多云",week: "11日星期五",direction:"无持续风向",windLeve: "向微风级",},]},/*** 生命周期函数--监听页面加载*/onLoad(options) {},/*** 生命周期函数--监听页面初次渲染完成*/onReady() {},/*** 生命周期函数--监听页面显示*/onShow() {},/*** 生命周期函数--监听页面隐藏*/onHide() {},/*** 生命周期函数--监听页面卸载*/onUnload() {},/*** 页面相关事件处理函数--监听用户下拉动作*/onPullDownRefresh() {},/*** 页面上拉触底事件的处理函数*/onReachBottom() {},/*** 用户点击右上角分享*/onShareAppMessage() {}})

weather.wxss

/* pages/weather/weather.wxss */
page {background-color: #001f3e;color: white;
}.content {font-family: 微软雅黑, 宋体;font-size: 14px;background-size: cover;height: 100%;width: 100%;color: #333333;
}.today {padding-top: 70rpx;height: 50%;
}.temp {font-size: 80px;text-align: center;
}.city {font-size: 20px;text-align: center;margin-top: 20rpx;margin-right: 10rpx;
}.lowhigh {font-size: 12px;text-align: center;margin-top: 30rpx;
}.type {font-size: 16px;text-align: center;margin-top: 30rpx;
}.week {font-size: 12px;text-align: center;margin-top: 30rpx;
}.weather {font-size: 12px;text-align: center;margin-top: 20rpx;
}.viewLine {width: 100%;height: 1rpx;background-color: #8b9da9;margin-top: 30rpx;
}.otherWeather {display: flex;justify-content: space-around;margin-top: 30rpx;padding-top: 20rpx;
}.infoOther {text-align: center;
}.infoOther view,.tip {padding: 10rpx;color: #c1cad4;font-size: 12px;
}
.tip {margin-top: 30rpx;
}

但是注意此时我们采用的方式是写死的。一般来说,正常的开发应该是有网络请求的。

 这里我们这个接口来做这个案例http://ajax-api.itheima.net/api/weather?city=武汉

 我们看一下官方文档如何介绍request这个使用方式

 好的我们填入进去,但是发现报错了。

 勾选上这个,不校验合法域名

我们看一下它都返回了什么数据

 然后我们就可以把写死的值修改为服务器返回来的值

 看这原来的值就变成了服务器返回来的值。同理,我们可以采用这种办法,修改其他固定的值

  

最后我们在根据res.data这个数组中的值依此修改。如果觉得太过于繁琐。那么可以在data中用list:[]来接收这个返回来的值

 ok,完成开发。这个就是实时更新的小程序了 

项目地址

Chicksqace/Wx-Wearther: 采用api接口做的实时更新的天气预报微信小程序 (github.com)

http://www.lryc.cn/news/68288.html

相关文章:

  • css渐变
  • 《斯坦福数据挖掘教程·第三版》读书笔记(英文版) Chapter 2 MapReduce and the New Software Stack
  • HTML零基础快速入门(详细教程)
  • Kubernetes第5天
  • RK3568平台开发系列讲解(调试篇)debugfs 分析手段
  • 【Spring框架全系列】SpringBoot配置日志文件
  • 事务 ---MySQL的总结(六)
  • 22 标准模板库STL之容器适配器
  • 目标检测YOLO实战应用案例100讲-基于深度学习的自动驾驶目标检测算法研究
  • 服务网关Gateway
  • (4)定时器
  • 项目实现读写分离操作(mysql)
  • VP记录:Educational Codeforces Round 148 (Rated for Div. 2) A~D1
  • 无线模块|如何选择天线和设计天线电路
  • (11)LCD1602液晶显示屏
  • 类和对象下
  • 【云计算•云原生】4.云原生之什么是Kubernetes
  • 云厂商降价潮背后:来中小企业战场「拼刺刀」
  • 2-单片机GPIO相关知识点及流水灯和按键采集小实验
  • 基础知识(王爽老师书第一章)
  • 非煤矿山电子封条建设算法 yolov8
  • 七大软件架构设计原则详解
  • 【Python入门】Python循环语句(while循环的嵌套应用)
  • 数据来源和搜集
  • Python入门(七)if语句(二)
  • [元带你学: eMMC完全解读 2] eMMC协议相关术语与定义
  • 预测杭州五一黄金周的旅游出行人数
  • 内防泄密重要,还是外防窃密重要?
  • ChatGPT:2. 使用OpenAI创建自己的AI网站:1. 初探API
  • 5月17日,今日信息差