微信小程序 實(shí)例開(kāi)發(fā)總結(jié)
微信小程序 開(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)文章
基于JavaScript實(shí)現(xiàn)高德地圖和百度地圖提取行政區(qū)邊界經(jīng)緯度坐標(biāo)
本文給大家介紹javascript實(shí)現(xiàn)高德地圖和百度地圖提取行政區(qū)邊界經(jīng)緯度坐標(biāo)的相關(guān)知識(shí),本文實(shí)用性非常高,代碼簡(jiǎn)單易懂,需要的朋友參考下吧2016-01-01Javarscript中模塊(module)、加載(load)與捆綁(bundle)詳解
這篇文章主要給大家介紹了關(guān)于Javarscript中模塊(module)、加載(load)與捆綁(bundle)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-05-05javascript 設(shè)為首頁(yè)與加入收藏兼容多瀏覽器代碼
javascript 設(shè)為首頁(yè)與加入收藏兼容多瀏覽器代碼,不過(guò)由于ie7的特殊性,設(shè)為首頁(yè)不能使用,不過(guò)其它基于ie內(nèi)核的瀏覽器都是支持的。2011-01-01JavaScript數(shù)組方法的錯(cuò)誤使用例子
在本篇文章中我們給大家分享了幾種使用JavaScript數(shù)組容易出錯(cuò)的例子,需要的朋友們可以參考下。2018-09-09可以用鼠標(biāo)拖動(dòng)的DIV實(shí)現(xiàn)思路及代碼
DIV可以拖動(dòng)的效果,想必大家都有見(jiàn)到過(guò)吧,在本文也為大家實(shí)現(xiàn)一個(gè)不錯(cuò)的可以用鼠標(biāo)拖動(dòng)的div,感興趣的各位不要錯(cuò)過(guò)2013-10-10