自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從微信小程序到鴻蒙JS開發(fā)【02】-數(shù)據(jù)綁定&tabBar&swiper

開發(fā)
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com/#zz

[[380592]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com/#zz

1、鴻蒙的數(shù)據(jù)綁定

微信小程序的數(shù)據(jù)綁定是類似于Vue的,wxml文件用 {{ }} 和對應(yīng)js文件中的data對象中的屬性進(jìn)行綁定。

  1. <view class="city"
  2.     {{ now.location.name }}市 
  3. </view

  1. data: { 
  2.     now: { 
  3.       location: { 
  4.         name"南京" 
  5.       }, 
  6.   } 

  

那么鴻蒙中是否也是這樣綁定呢?嘗試在hml文件的div標(biāo)簽中使用 {{ }} 綁定js文件中的屬性值,但卻什么都沒有顯示。

  1. <!--錯誤代碼--> 
  2. <div class="container"
  3.     <div class="top"
  4.         <div class="topItem"
  5.             {{t1}} 
  6.         </div> 
  7.         <div class="topItem"
  8.             {{t2}} 
  9.         </div> 
  10.         <div class="topItem"
  11.             {{t3}} 
  12.         </div> 
  13.     </div> 
  14. ... 
  15. </div> 
  1. export default { 
  2.     data: { 
  3.         t1: "吃飯"
  4.         t2: "睡覺"
  5.         t3: "打豆豆" 
  6.     } 

 

其實是因為div標(biāo)簽中直接放文字是不會顯示的,需要將文字放在標(biāo)簽中才會顯示出來。將hml文件做些更改,可以看到數(shù)據(jù)已被綁定到頁面中了。

  1. <div class="container"
  2.     <div class="top"
  3.         <div class="topItem"
  4.             <text> 
  5.                 {{t1}} 
  6.             </text> 
  7.         </div> 
  8.         <div class="topItem"
  9.             <text> 
  10.                 {{t2}} 
  11.             </text> 
  12.         </div> 
  13.         <div class="topItem"
  14.             <text> 
  15.                 {{t3}} 
  16.             </text> 
  17.         </div> 
  18.     </div> 
  19. ... 
  20. </div> 

在一個數(shù)組中循環(huán)取值的方式和微信小程序也是類似的,可用一個標(biāo)簽作為邏輯控制塊,其屬性有for和if。需注意循環(huán)的每一項索引為$idx,值為$item。需要使用'$'去引用,且沒有類似于wx:for-item等屬性去改變變量名。若要重命名,可寫為for="{{ (index, value) in ... }}

  1. <div class="content"
  2.     <div class="contentItem"
  3.         <block for="{{array}}"
  4.             <div class="item"
  5.                 <text>{{$idx}}: {{$item}}</text> 
  6.             </div> 
  7.         </block> 
  8.     </div> 
  9. </div> 
  1. export default { 
  2.     data: { 
  3.         t1: "吃飯"
  4.         t2: "睡覺"
  5.         t3: "打豆豆"
  6.         array: [1, 3, 5, 7, 9, 2, 4, 6, 8] 
  7.     } 

2、自定義tabBar

在微信小程序中可以直接在app.json中定義一個tabBar。

  1. "tabBar": { 
  2.     "color""#333333"
  3.     "backgroundColor""#fdfdfd"
  4.     "selectedColor""#E20A0B"
  5.     "list": [ 
  6.       { 
  7.         "pagePath""pages/weather/weather"
  8.         "text""天氣"
  9.         "iconPath""icon/weather.png"
  10.         "selectedIconPath""icon/weather1.png" 
  11.       }, 
  12.     ... 
  13.     ] 

 

鴻蒙沒有這種在json中繼承的配置項,但我們可以用flex布局自己寫一個,甚至可以加上動畫等更豐富的功能??紤]到每一個菜單項有選中和未選中兩種狀態(tài),各需準(zhǔn)備兩張圖片。將圖片放在/entry/src/main/js/default/common文件夾中,并在js文件中定義菜單欄數(shù)據(jù)。此處需要注意雖然在目錄結(jié)構(gòu)上common文件夾和頁面js文件存在父級目錄的關(guān)系,但在js加載時common被認(rèn)定為同一級目錄,圖片目錄定義處需注意。

  1. export default { 
  2.     data: { 
  3.         tabBar: [ 
  4.             { 
  5.                 text: "天氣"
  6.                 img1: "./common/icon/weather.png"
  7.                 img2: "./common/icon/weather1.png" 
  8.             }, 
  9.             { 
  10.                 text: "每日新聞"
  11.                 img1: "./common/icon/news.png"
  12.                 img2: "./common/icon/news1.png" 
  13.             }, 
  14.             { 
  15.                 text: "本地新聞"
  16.                 img1: "./common/icon/local.png"
  17.                 img2: "./common/icon/local1.png" 
  18.             }, 
  19.             { 
  20.                 text: "查詢"
  21.                 img1: "./common/icon/search2.png"
  22.                 img2: "./common/icon/search1.png" 
  23.             } 
  24.         ], 
  25.         barIdx: 0, 
  26.     } 

 

頁面設(shè)計上,采用position: fixed;將菜單欄固定在頁面底部,并結(jié)合flex布局使頁面美觀。判斷當(dāng)前選中哪一項,則可以使用三元表達(dá)式。

  1. <!-- 底部菜單欄 --> 
  2.     <div class="tabBar"
  3.         <block for="{{ tabBar }}"
  4.             <div class="cell" onclick="changeMenu($idx)"
  5.                 <div class="image"
  6.                     <image src="{{ barIdx == $idx ? $item.img2: $item.img1 }}"></image> 
  7.                 </div> 
  8.                 <div class="text"
  9.                     <text class="{{ barIdx == $idx ? 'a' : 'b' }}"
  10.                         {{ $item.text }} 
  11.                     </text> 
  12.                 </div> 
  13.             </div> 
  14.         </block> 
  15.     </div> 

  1. /*底部菜單*/ 
  2. .tabBar { 
  3.     width: 100%; 
  4.     height: 170px; 
  5.     position: fixed; 
  6.     bottom: 0px; 
  7.     border-top: 1px solid #444444; 
  8.     display: flex; 
  9.     justify-content: space-around; 
  10.     align-items: center; 
  11.     background-color: #f5f5f5; 
  12. .cell { 
  13.     width: 20%; 
  14.     height: 160px; 
  15.     display: flex; 
  16.     flex-direction: column
  17. .image { 
  18.     width: 100%; 
  19.     height: 110px; 
  20.     display: flex; 
  21.     justify-content: center; 
  22.     align-items: center; 
  23. .image>image { 
  24.     width: 90px; 
  25.     height: 90px; 
  26. .a { 
  27.     color: #0074DD; 
  28. .b { 
  29.     color: #333333; 
  30. .text { 
  31.     width: 100%; 
  32.     height: 50px; 
  33.     display: flex; 
  34.     justify-content: center; 
  35.     align-items: center; 
  36. .text>text { 
  37.     font-size: 35px; 

div的點擊事件處理屬性為onclick,其不會像微信小程序一樣自動傳入一個事件對象,而需要我們自行定義傳入的參數(shù)。如上的onclick="changeMenu($idx)"就是鴻蒙傳入點擊事件的方法。這個函數(shù)只需要改變barIdx的值便可以實現(xiàn)點擊切換tabBar對應(yīng)項的顏色和圖片,達(dá)到“四兩撥千斤”的效果。

  1. changeMenu(idx) { 
  2.         this.barIdx = idx; 

 這里又出現(xiàn)了和微信小程序的不同處,微信小程序改變data中的值需要使用wx.setData()函數(shù)進(jìn)行設(shè)置,而鴻蒙中直接使用this.key = value即可。

點一下其他菜單項:

 

3、結(jié)合swiper進(jìn)行翻頁

tabBar完成了,但這個菜單欄是寫在一個頁面中的,要怎樣進(jìn)行翻頁呢?有一個在一個js頁面中實現(xiàn)“翻頁”的方式,就是結(jié)合swiper。和微信小程序中的swiper組件一樣,它是一個可滑動的組件,多用于輪播圖、滾動通知等。

鴻蒙的swiper需要定義一個頁面唯一的id屬性,用于點擊事件聯(lián)動頁面滑動。index屬性為當(dāng)前的索引值。

  1. <!-- 劃頁swiper --> 
  2.     <swiper id="pager" index="0" class="pager" onchange="changePage" indicator="false"
  3.     <!--4個div作為4頁--> 
  4.     </swiper> 

  1. /*劃頁swiper*/ 
  2. .pager { 
  3.     width: 100%; 
  4.     height: 100%; 
  5. .pager>div { 
  6.     display: flex; 
  7.     flex-direction: column

 現(xiàn)需要實現(xiàn)兩個功能,滑動swiper實現(xiàn)tabBar聯(lián)動樣式變化,以及點擊tabBar中的項聯(lián)動swiper頁面滑動。更改changeMenu方法:

  1. changeMenu(idx) { 
  2.     this.barIdx = idx; 
  3.     this.$element("pager").swipeTo({ 
  4.         index: idx 
  5.     }); 

 鴻蒙通過this.$element(id)找到頁面中對應(yīng)id的組件,如為swiper組件則可使用swipeTo()方法實現(xiàn)滑動,其index屬性則為滑動到的頁面索引值(0開始)。

changePage方法,只需要改變barIdx的值即可。通過swiper的onchange屬性綁定方法名,滑動到的index的值會作為event.index被傳入。

  1. changePage(event) { 
  2.     this.barIdx = event.index

 大功告成。

©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請注明出處,否則將追究法律責(zé)任。

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com/#zz

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-02-21 11:09:18

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 12:25:26

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-03-02 09:29:29

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-20 09:52:02

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 12:23:57

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-22 14:56:55

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-25 10:01:19

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-04 13:49:41

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 09:52:42

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-07 09:17:24

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-25 15:13:08

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-24 09:36:03

鴻蒙CSS應(yīng)用開發(fā)

2016-11-04 10:48:37

信小程序

2016-09-27 16:38:24

JavaScript微信Web

2016-11-04 10:49:48

微信小程序

2017-05-08 15:03:07

微信小程序開發(fā)實戰(zhàn)

2016-09-28 18:10:59

微信程序MINA

2016-09-27 20:36:23

微信HttpWeb

2016-11-04 10:30:17

微信小程序

2018-09-11 10:32:07

云開發(fā)小程序開發(fā)者
點贊
收藏

51CTO技術(shù)棧公眾號