亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序 實(shí)例開(kāi)發(fā)總結(jié)

 更新時(shí)間:2017年04月26日 14:40:21   投稿:lqh  
這篇文章主要介紹了微信小程序 開(kāi)發(fā)過(guò)程中遇到問(wèn)題總結(jié)的相關(guān)資料,需要的朋友可以參考下

微信小程序 開(kāi)發(fā)過(guò)程中遇到問(wèn)題總結(jié)

第一次正式開(kāi)發(fā)一個(gè)小程序,就從以下幾個(gè)方面來(lái)談一談小程序的開(kāi)發(fā)過(guò)程和心得吧,主要說(shuō)說(shuō)這次項(xiàng)目中用到的功能。

數(shù)據(jù)請(qǐng)求

這次的小程序,沒(méi)有太多的附加功能,所以數(shù)據(jù)以及對(duì)數(shù)據(jù)的處理是這次的主體工作,小程序向用戶提供API,供用戶向自己的服務(wù)器請(qǐng)求數(shù)據(jù),值得一提的是,開(kāi)發(fā)小程序之前,需要先在微信公眾平臺(tái)申請(qǐng)appID,并且綁定域名,域名必須是https協(xié)議,然后在小程序的開(kāi)發(fā)工具的配置信息中完善信息,請(qǐng)求的地址需要在前面綁定的域名下。這個(gè)項(xiàng)目中用到wx.request從服務(wù)器拉取數(shù)據(jù)。

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();
     }
   }
 });

數(shù)據(jù)緩存

這里使用數(shù)據(jù)緩存是因?yàn)樾枰鲆粋€(gè)搜索功能,就涉及到頁(yè)面之間的數(shù)據(jù)傳遞,放在地址中也是一種方法,借用一下localStorage也可以,使用wx.setStorage將數(shù)據(jù)存儲(chǔ)到localStorage中,頁(yè)面跳轉(zhuǎn)之后,在從localStorage中讀取就可以了,讀取數(shù)據(jù)的時(shí)候分同步讀取和異步讀取。

剪切板的應(yīng)用

借用小程序的API可以很方便的將任何信息復(fù)制到剪切板,然后就可以粘貼了。

wx.setClipboardData({
   data: '【' + that.data.couponData.list[e.currentTarget.id].goodsTitle + '】復(fù)制這條信息,打開(kāi)【手機(jī)淘寶】' + 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})
   }
 });

模板

在這個(gè)項(xiàng)目中,頁(yè)面基本很相似,但有細(xì)微差別,所以就使用了模板,新建一個(gè)template/template.wxml,name屬性必須要設(shè)置。

 <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='請(qǐng)輸入關(guān)鍵詞找券' 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>

模塊化

對(duì)于公共的js可以寫(xiě)在一個(gè)專門(mén)的js文件中,然后使用module.exports暴露接口。
通用的js文件使用require引入。

 var common = require('../../common/common.js');
 ...
 common.f(); //調(diào)用

redirectTo & navigateTo

redirectTo是重定向至某頁(yè)面,navigateTo是打開(kāi)新的頁(yè)面,值得說(shuō)明的一點(diǎn)是,使用navigateTo打開(kāi)的頁(yè)面太多會(huì)導(dǎo)致小程序卡頓。

分享

 Page({
   onShareAppMessage: function () {
     return {
       title: 'your title!',
       path: '/xxxx/xxxx/xxxx',  //分享之后回到這個(gè)頁(yè)面
       success: function(res) {
         f(); //成功回調(diào);
       },
       fail: function(res) {
         f(); //失敗回調(diào);

       }
     }
   }
 })

提高列表滑動(dòng)的流暢性

簡(jiǎn)而言之就是頁(yè)面滾動(dòng)到哪里,列表中的圖片就顯示到哪里,實(shí)現(xiàn)方法如下。

//js文件
 Page({
   loadImg:function(e) {
     //計(jì)算接下來(lái)加載哪幾張
     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; //列表中的每一項(xiàng)有一個(gè)標(biāo)記是否加載圖片的的屬性,默認(rèn)false,隨著頁(yè)面滾動(dòng),一個(gè)個(gè)變成true。
       }
     }
     this.setData({"couponData.list":temp});
     temp = null;
   },
 })

 //wxml文件中在scroll-view上綁定事件。
 <scroll-view class="section" scroll-y="true" bindscroll='loadImg'></scroll-view>

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論