当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序 实例开发总结

微信小程序 实例开发总结

2018年05月10日  | 移动技术网IT编程  | 我要评论
微信小程序 开发过程中遇到问题总结 第一次正式开发一个小程序,就从以下几个方面来谈一谈小程序的开发过程和心得吧,主要说说这次项目中用到的功能。 数据请求 这次的小

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

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

数据请求

这次的小程序,没有太多的附加功能,所以数据以及对数据的处理是这次的主体工作,小程序向用户提供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>

模块化

对于公共的js可以写在一个专门的js文件中,然后使用module.exports暴露接口。
通用的js文件使用require引入。

 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>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网