网友真实露脸自拍10p,成人国产精品秘?久久久按摩,国产精品久久久久久无码不卡,成人免费区一区二区三区

小程序模板網(wǎng)

微信小程序:漫畫小程序項(xiàng)目總結(jié)

發(fā)布時(shí)間:2018-08-01 09:59 所屬欄目:小程序開發(fā)教程

項(xiàng)目考察點(diǎn):

1、wx:for 循環(huán)的使用。 
2、this.setData 內(nèi)獲取動態(tài)數(shù)組數(shù)據(jù)的方式。 
3、難點(diǎn):在有旁白內(nèi)容時(shí),點(diǎn)擊旁白隱藏內(nèi)容,點(diǎn)擊圖片不處理事件(通過自定義 data-tag 區(qū)分,用e.currentTarget.dataset 選取子節(jié)點(diǎn))。 
4、e.currentTarget.dataset、e.target.dataset 用法,console.log(e) 調(diào)試技巧。 
5、難點(diǎn):數(shù)組數(shù)據(jù)下標(biāo)為動態(tài)數(shù)據(jù)時(shí)的取值方法。

 

項(xiàng)目要求:

1、構(gòu)建 WXML 模板。 
2、使用 wx:for 循環(huán)輸出四個(gè)圖片,每個(gè)圖片包含一個(gè) view 組件。每個(gè) view 中又包含一個(gè) image 組件展示圖片和一個(gè) text 組件展示漫畫旁白。 
3、循環(huán)結(jié)構(gòu)通過 template 形成獨(dú)立模板文件。 
4、為 view 組件綁定事件(其它組件不做事件綁定),默認(rèn)不展示旁白,當(dāng)點(diǎn)擊圖片時(shí)可以查看旁白;而在有旁白內(nèi)容時(shí),點(diǎn)擊旁白隱藏內(nèi)容,點(diǎn)擊圖片不處理事件。 
5、旁白內(nèi)容在 WXML 里展示,需要使用 WXS 處理,把所有半角逗號 , 改為全角逗號 ,,WXS 以獨(dú)立的文件存在。

 

wx:for 循環(huán)的使用

index.js:

 

				
  1. Page({
  2. data: {
  3. images: [{
  4. src: '/images/dov-1.png',
  5. text: '',
  6. aside: false,
  7. unique: '0'
  8. }, {
  9. src: '/images/dov-2.png',
  10. text: '過年浪一浪,爆竹好,棒棒',
  11. aside: false,
  12. unique: '1'
  13. }, {
  14. src: '/images/dov-3.png',
  15. text: '突然一個(gè)想不開,原地爆炸好心塞',
  16. aside: false,
  17. unique: '2'
  18. }, {
  19. src: '/images/dov-4.png',
  20. text: '嚇?biāo)腊仔軐殞?變成熊貓大佬',
  21. aside: false,
  22. unique: '3'
  23. }]
  24. },
  25. //......
  26. })

index.wxml:

 

				
  1. <import src="template.wxml"/> <view class="container"> <template is="imgItem" data="{{images}}" /> </view>

由于循環(huán)結(jié)構(gòu)通過 template 形成獨(dú)立模板文件,因此 wxml 文件只需引入模板。{{images}} 為 js 文件data中的 images。imgItem 對應(yīng)模板文件中的 name 屬性。

模板template:

 

				
  1. <block wx:for="{{images}}" wx:for-item="item" wx:for-index="index" wx:key="unique"></block>

wx:for-item 指定當(dāng)前項(xiàng)變量名,wx:for-index 指定數(shù)組下標(biāo)變量。如果不指定 wx:for-item 和 wx:for-index,數(shù)組當(dāng)前項(xiàng)的變量名默認(rèn)為 item,默認(rèn)數(shù)組的當(dāng)前項(xiàng)的下標(biāo)變量名默認(rèn)為 index。(文檔戳這:小程序列表渲染)

因此項(xiàng)目中 images[index] 可以用 item 代替。

template.wmxl:

 

				
  1. <wxs src="index.wxs" module="tools" />
  2.  
  3. <template name="imgItem">
  4. <view wx:for="{{images}}" wx:key="unique" bind:tap="toggleText" data-value="{{item.aside}}" data-unique="{{item.unique}}">
  5. <image src="{{item.src}}" data-tag="image" />
  6. <text class="{{item.aside?'textShow':'textHide'}}" data-tag="text">{{tools.commaReplace(item.text)}}</text>
  7. </view>
  8. </template>

項(xiàng)目要求通過 wxs 處理半角變?nèi)嵌禾枂栴},引入 wxs 用法是,其中 tools 是 wxs 的名稱,通過 module 定義。在{{tools.commaReplace(item.text)}}中,.commaReplace 由 wxs 定義,() 中傳入 js 數(shù)據(jù)。

 

wxs:

在小程序中, 由于運(yùn)行環(huán)境的差異,在 iOS 設(shè)備上小程序內(nèi)的 wxs 會比 javascript 代碼快 2 ~ 20 倍。

index.wxs:

 

				
  1. function commaReplace(some_msg){
  2. while (some_msg.indexOf(",") != -1) {//尋找每一個(gè)英文逗號,并替換
  3. some_msg = some_msg.replace(",", ",");
  4. }
  5. return some_msg;
  6. }
  7. module.exports = {
  8. commaReplace: commaReplace
  9. };

使用 while 循環(huán)遍歷字符串中每個(gè)字符的是否與半角逗號 , 匹配,如匹配使用 replace(",", ",") 方法替換,module.exports 輸出模板數(shù)據(jù)供 wxml 調(diào)用。

 

旁白的顯示與隱藏

index.js:

 

				
  1. toggleText: function (e){
  2. console.log(e);
  3. var tag = e.target.dataset.tag;
  4. var index = e.currentTarget.dataset.unique;
  5. var value = (!e.currentTarget.dataset.value);
  6. var aside = 'images[' + index + '].aside';//設(shè)置images數(shù)組動態(tài)變量aside
  7. console.log(value);
  8. if (tag === 'image'){
  9. if (!this.data.images[index].aside){
  10. value = true;
  11. this.setData({
  12. [aside]: value
  13. })
  14. }
  15. } else if (tag === 'text'){
  16. value = false; this.setData({
  17. [aside]: value
  18. })
  19. }
  20. }

在小程序里,想要更新頁面的數(shù)據(jù),必須使用 this.setData 對數(shù)據(jù)進(jìn)行更新。

在知道數(shù)組下標(biāo)或?qū)傩宰侄蚊Q的情況下,可以這樣寫:

 

				
  1. this.setData({ 'images[0].aside': this.data.images[@].aside})

數(shù)組數(shù)據(jù)下標(biāo)為動態(tài)數(shù)據(jù)時(shí)的取值方法:

項(xiàng)目中我采用的方式是給 aside 賦值,然后在 this.setData 中輸出 [aside] 的方式。

 

				
  1. var aside = 'images[' + index + '].aside';//設(shè)置images數(shù)組動態(tài)變量aside

還有另一種更高級的寫法,使用 JSON. stringify 把要設(shè)置的數(shù)據(jù)進(jìn)行序列化,或者使用字符串拼接的方法拼出 json ,然后再重新JSON.parse 傳給 setData:

 

				
  1. let json ='{"images[' + index +'].aside":'+ this .data .images[index] .aside.toString() +'}';this.setData(JSON.parse(json));
 

console.log(e) 調(diào)試技巧:立正小歪牙

e.target 觸發(fā)事件的組件的一些屬性值集合,e.currentTarget 則是當(dāng)前組件的一些屬性值集合。

在不知道什么情況使用 e.currentTarget.dataset 還是 e.target.dataset 時(shí),可以通過控制臺打印 console.log(e) ,然后分別查看 .target 和 .currentTarget 來找到想要的屬性值。

觸發(fā) toggleText 時(shí)的 console.log(e) 打印信息.



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://www.xiuhaier.com/wxmini/doc/course/24679.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×