博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小程序开发系列(三)数据交互与渲染
阅读量:4584 次
发布时间:2019-06-09

本文共 2837 字,大约阅读时间需要 9 分钟。

微信小程序的api中提供了网络交互的api,我们只要调用即可和后端进行数据交互,该api为wx.request.,具体代码如下。

//list.js//获取应用实例var app = getApp()Page({  data: {    list:[],    hiddenLoading: true,    url: ''  },  loadList: function () {    var that = this;    that.setData({      hiddenLoading: !that.data.hiddenLoading    })    var url = app.urls.CloudData.getList;    that.setData({      url: url    });    wx.request({      url: url,      data: {},      method: 'GET',      success: function (res) {        var list= res.data.list;        if (list == null) {          list = [];        }        that.setData({          list: list,          hiddenLoading: !that.data.hiddenLoading        });         wx.showToast({          title: "获取数据成功",          icon:  'success',          duration: 2000        })      },      fail: function (e) {        var toastText='获取数据失败' + JSON.stringify(e);        that.setData({          hiddenLoading: !that.data.hiddenLoading        });        wx.showToast({          title: toastText,          icon:  '',          duration: 2000        })      },      complete: function () {        // complete      }    }),  //事件处理函数  bindViewTap: function () {    wx.navigateTo({      url: '../logs/logs'    })  },  onLoad: function () {  },  onReady: function () {    this.loadList();  },  onPullDownRefresh: function () {    this.loadList();    wx.stopPullDownRefresh()  }})
在loadList函数中进行了网络请求,请求的数据放到了data的list中。我们使用setData来修改list,在该函数调用之后,微信小程序的框架就会判断数据状态的变化,然后进行diff判断,如果有变化就渲染到界面中。这个与react.js的渲染方式相似,主要是内部维护了一个类似于虚拟文档的对象,然后通过对虚拟文档的判断来呈现界面,这样可以大大提高性能。

这里我们还做了一个下拉刷新的触发,即onPullDownRefresh函数,为了能够使用下拉刷新,我们需要进行配置,现在我们只需要当前页面生效,所以只要在对应页的json中配置即可,即在list.json中配置。

list.json

{    "navigationBarTitleText": "产品列表",        "enablePullDownRefresh":true}
如果需要所有的页面的生效,可以在app.json中的window中配置。

app.json

{  "pages":[    "pages/index/index",    "pages/logs/logs",    "pages/list/list"  ],  "window":{    "backgroundTextStyle":"light",    "navigationBarBackgroundColor": "#fff",    "navigationBarTitleText": "WeChat",    "navigationBarTextStyle":"black",    "enablePullDownRefresh":true  }}
在app.json中,还有一个pages,我们需要路由的页面都需要在这里注册,否则无法路由到。

在请求数据的时候,加入了等待和获取成功失败的提示。这需要相应的页面配合,页面代码list.wxm.如下

{ {item.no}}({ {item.content}})
/**list.wxss**/.widget {  position: relative;  margin-top: 5rpx;  margin-bottom: 5rpx;  padding-top: 10rpx;  padding-bottom: 10rpx;  padding-left: 40rpx;  padding-right: 40rpx;  border: #ddd 1px solid;}
/**app.wxss**/.container {  height: 100%;  display: flex;  flex-direction: column;  align-items: center;  justify-content: space-between;  box-sizing: border-box;  padding-top: 10rpx;  padding-bottom: 10rpx;}
最终的呈现效果

欢迎打描左侧二维码打赏。

转载请注明出处

转载于:https://www.cnblogs.com/sparkleDai/p/7604912.html

你可能感兴趣的文章
小鸟哥哥博客 For SAE
查看>>
gui编程实践(3)--记事本界面 JMenuBar JMenu
查看>>
App测试方法总结
查看>>
51nod-1228: 序列求和
查看>>
BZOJ1303: [CQOI2009]中位数图
查看>>
2015上海马拉松线上跑感悟-人生如同一场马拉松
查看>>
北航软院2013级C#期末考试部分考题解答
查看>>
CentOS 系统中安装 ArcGIS Server10.1 一些问题及解决
查看>>
asp.net里登陆记住密码
查看>>
【BZOJ】2190 [SDOI2008]仪仗队(欧拉函数)
查看>>
线性规划中的单纯形法与内点法(原理、步骤以及matlab实现)(一)
查看>>
简单DP【p2758】编辑距离
查看>>
Spring Data JPA:关联映射操作
查看>>
JWT入门简介
查看>>
结对编程——吐槽必应词典
查看>>
katalon系列八:Katalon Studio图片识别
查看>>
Spring操作指南-针对JDBC配置声明式事务管理(基于XML)
查看>>
sql server 调优----索引缺失
查看>>
spring + junit 测试
查看>>
.net core 无法获取本地变量或参数的值,因为它在此指令指针中不可用,可能是因为它已经被优化掉了...
查看>>