微信小程序开发实例总结

互联网 17-5-29
这篇文章主要介绍了微信小程序 开发过程中遇到问题总结的相关资料,需要的朋友可以参考下

微信小程序 开发过程中遇到问题总结

第一次正式开发一个小程序,就从以下几个方面来谈一谈小程序的开发过程和心得吧,主要说说这次项目中用到的功能。

这次的小程序,没有太多的附加功能,所以数据以及对数据的处理是这次的主体工作,小程序向用户提供API,供用户向自己的服务器请求数据,值得一提的是,开发小程序之前,需要先在微信公众平台申请appID,并且绑定域名,域名必须是https协议,然后在小程序的开发工具的配置信息中完善信息,请求的地址需要在前面绑定的域名下。这个项目中用到wx.request从服务器拉取数据。

wx.request({     url: that.data.couponData.requestUrl,     data: that.data.couponData.queryData,     header: {       'content-type': 'application/json'     },     success: function(res) {       var list = res.data.goodsList;       console.log(res.data);       for(var i in list) {         list[i].quanUsedNum = parseInt(list[i].quanTotalNum) - parseInt(list[i].quanRemainNum);        list[i].isImgRendered = false;       }      list[0].isImgRendered = list[1].isImgRendered = list[2].isImgRendered = list[3].isImgRendered = true;       that.setData({"couponData.totalPage":res.data.totalPage});       that.setData({"couponData.list":that.data.couponData.list.concat(list)});      that.setData({"couponData.loadmore":!that.data.couponData.loadmore});       that.setData({"couponData.queryData.pageNum":parseInt(that.data.couponData.queryData.pageNum) + 1});       if(that.data.couponData.queryData.pageNum > that.data.couponData.totalPage) {        that.setData({"couponData.isAction":false});      }        if(that.data.couponData.list.length < 1) {        that.setData({"couponData.nodata":true});      }       if(f) {         f();       }     }   });

数据缓存

这里使用数据缓存是因为需要做一个搜索功能,就涉及到页面之间的数据传递,放在地址中也是一种方法,借用一下localStorage也可以,使用wx.setStorage将数据存储到localStorage中,页面跳转之后,在从localStorage中读取就可以了,读取数据的时候分同步读取和异步读取。

剪切板的应用

借用小程序的API可以很方便的将任何信息复制到剪切板,然后就可以粘贴了。

wx.setClipboardData({     data: '【' + that.data.couponData.list[e.currentTarget.id].goodsTitle + '】复制这条信息,打开【手机淘宝】' + that.data.couponData.list[e.currentTarget.id].twoInOneKouling,     success: function(res) {       that.setData({"couponData.copyTip":true,"couponData.Kouling":that.data.couponData.list[e.currentTarget.id].twoInOneKouling})     }   });

模板

在这个项目中,页面基本很相似,但有细微差别,所以就使用了模板,新建一个template/template.wxml,name属性必须要设置。

   <template name='navsearch'>   <view class='nav-search'>     <view class='nav-search__container space-between'>       <view class='nav-search__search' wx:if='{{isSearch}}'></view>       <input class='nav-search__input' placeholder='请输入关键词找券' name='queryStr' value="{{queryStr}}" bindfocus='toggleSearch' bindconfirm='doQuery' bindinput="syncQuery"/>       <view class='nav-search__delete' wx:if='{{!isSearch}}' bindtap='deleteAll'></view>       <view class='nav-search__btn center' wx:if='{{!isSearch}}' bindtap='doQuery'>搜索</view>     </view>       <view class='nav-filter' bindtap='toggleFilter'></view>   </view>   </template>     <!--在其他文件中使用模板-->   <import src="/template/template.wxml" />   <template is='navsearch' data="{{...couponData}}"></template>

模块化

   var common = require('../../common/common.js');   ...   common.f(); //调用

redirectTo & navigateTo

redirectTo是重定向至某页面,navigateTo是打开新的页面,值得说明的一点是,使用navigateTo打开的页面太多会导致小程序卡顿。

分享

   Page({     onShareAppMessage: function () {       return {         title: 'your title!',         path: '/xxxx/xxxx/xxxx',  //分享之后回到这个页面         success: function(res) {           f(); //成功回调;         },         fail: function(res) {           f(); //失败回调;           }       }     }   })

提高列表滑动的流畅性

简而言之就是页面滚动到哪里,列表中的图片就显示到哪里,实现方法如下。

//js文件   Page({     loadImg:function(e) {       //计算接下来加载哪几张       var index = Math.floor((e.detail.scrollTop - 8)/259.5);       var temp = this.data.couponData.list; //完整的列表       var min = Math.max(index * 2,0),max = Math.min(index * 2 + 8,temp.length);       for(var i = min; i < max; i ++) {         if(temp[i] && !temp[i].isImgRendered) {           temp[i].isImgRendered = true; //列表中的每一项有一个标记是否加载图片的的属性,默认false,随着页面滚动,一个个变成true。         }       }       this.setData({"couponData.list":temp});       temp = null;     },   })     //wxml文件中在scroll-view上绑定事件。   <scroll-view class="section" scroll-y="true" bindscroll='loadImg'></scroll-view>

以上就是微信小程序开发实例总结的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:分享一个微信群红包算法实例代码

相关资讯