小程序开发心得(一)

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

  • 数据请求

这次的小程序,没有太多的附加功能,所以数据以及对数据的处理是这次的主体工作,小程序向用户提供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>
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,323评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,066评论 25 709
  • 给提问的开发者的建议:提问之前先查询 文档、通过社区右上角搜索搜索已经存在的问题。 写一个简明扼要的标题,并且...
    极乐叔阅读 14,754评论 0 3
  • 微信小程序在无论在功能、文档及相关支持方面,都是优于前面几种微信账号类型,它提供了很多原生程序才有的接口,使得我们...
    未央大佬阅读 6,822评论 0 12
  • 1 目的将hadoop 2.7.1 安装到 166、167、168 三台机器上2 提供环境练习环境192.168....
    灼灼2015阅读 8,750评论 4 40