溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序如何實(shí)現(xiàn)模版渲染

發(fā)布時(shí)間:2021-07-06 09:31:32 來源:億速云 閱讀:156 作者:小新 欄目:web開發(fā)

這篇文章主要為大家展示了“微信小程序如何實(shí)現(xiàn)模版渲染”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“微信小程序如何實(shí)現(xiàn)模版渲染”這篇文章吧。

微信小程序的界面程序支持html語法,多加了一部分標(biāo)簽,如view、block、templete等。

模版渲染
index.wxml

<view>
 <p>{{helloWord}}</p>
</view>

其中{{}}里面包含的內(nèi)容你可以理解為一個(gè)變量,怎么讓程序解析出{{helloWord}}變量

在index.js 中注冊(cè)這個(gè)變量

var json = {
 data:{
  "helloWord" : "hello world"
 }
};

page(json)

然后我們運(yùn)行小程序,就可以發(fā)現(xiàn)顯示的就是hello world,即所有的變量都需要包含在注冊(cè)界面的data中
有的人可能會(huì)問,怎么去動(dòng)態(tài)的添加這些變量呢?

var json = {
 data:{
  "helloWorld":""
 },
 //監(jiān)聽頁面加載
 onLoad:function(){
  var that = this;
  that.setData({
   "helloWorld":"hello world"
  })
 }
};
page(json)

甚至我們還可以

var json = {
 data:{},
 //監(jiān)聽頁面加載
 onLoad:function(){
  var that = this;
  that.setData({
   "helloWorld":"hello world"
  })
 }
};
page(json)

都能實(shí)現(xiàn)相同效果,每次調(diào)用setData()函數(shù)的是夠都會(huì)重新渲染一次頁面。

index1.wxml

<view>
 <view wx:for="{{users}}" wx:for-item="{{item}}">
  <view wx:for="{{item}}" wx:for-index="{{key}}" wx:for-item="{{val}}">
    <p>{{key}}=>{{val}}</p>
  </view>
 </view>
 <view id="nameDemo">
  <p>name : {{users[0].name}}</p>
 </view>
 <view>
  <button bindtap="clickFunc">我是測(cè)試按鈕</button>
 </view>
</view>

index1.js

var json={
 data:{},
 //監(jiān)聽頁面顯示
 onShow:function(){
  vat that = this;
  that.setData({
   users:[
    {
     "name":"name1",
     "age":100
    },
    {
     "name":"name2",
     "age":101
    }
   ]
  });
 }
};
page(json);

其中變量that的作用是對(duì)this的作用域的一個(gè)擴(kuò)展。
wx:for 循環(huán)一個(gè)變量
wx:for-index 代表循環(huán)的鍵名
wx:for-item 代表循環(huán)的鍵值
users 在頁面顯示的時(shí)候動(dòng)態(tài)的添加到了data作用域中。

現(xiàn)在我們?cè)賮砜匆粋€(gè)新的問題 如上id=”nameDemo” view中{{users[0].name}} 這個(gè)值我們?cè)趺慈?dòng)態(tài)的更改問題
有的可能說直接重新生成一個(gè)json直接渲染進(jìn)去不就行了?
這種方案是可以的,但是要考慮到渲染的性能呀,如果每次調(diào)用都重新渲染一次,卡死你。
解決方案就是js的小技巧

只更改{{users[0].name}}的值

var json = {
 data:{},
 //監(jiān)聽頁面顯示
 onShow:function(){
  vat that = this;
  that.setData({
   users:[
    {
     "name":"name1",
     "age":100
    },
    {
     "name":"name2",
     "age":101
    }
   ]
  });
 },
 clickFunc:function(event){
  vat that = this;
  var dataJson = {};

  dataJson["users[0].name"] = "我是誰"; 
  that.setData(dataJson);
 }
}

其中bindtap 給button對(duì)象添加了一個(gè)點(diǎn)擊事件,點(diǎn)擊事件對(duì)應(yīng)的函數(shù)是clickFunc 參數(shù)event數(shù)據(jù)結(jié)構(gòu)如下

 { 
  "type": "tap", 
  "timeStamp": 1252, 
  "target": { 
   "id": "tapTest", 
   "offsetLeft": 0, 
   "offsetTop": 0
  }, 
  "currentTarget": { 
   "id": "tapTest", 
   "offsetLeft": 0, 
   "offsetTop": 0, 
   "dataset": { 
   "hi": "MINA" 
   } 
  }, 
  "touches": [{ 
   "pageX": 30, 
   "pageY": 12, 
   "clientX": 30, 
   "clientY": 12, 
   "screenX": 112, 
   "screenY": 151 
  }], 
  "detail": { 
   "x": 30, 
   "y": 12 
  } 
 }

以上是“微信小程序如何實(shí)現(xiàn)模版渲染”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI