這篇文章主要為大家展示了“微信小程序如何實現(xiàn)模版渲染”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“微信小程序如何實現(xiàn)模版渲染”這篇文章吧。
網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、成都小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了香洲免費建站歡迎大家使用!
微信小程序的界面程序支持html語法,多加了一部分標簽,如view、block、templete等。
模版渲染
index.wxml
<view> <p>{{helloWord}}</p> </view>
其中{{}}里面包含的內(nèi)容你可以理解為一個變量,怎么讓程序解析出{{helloWord}}變量
在index.js 中注冊這個變量
var json = { data:{ "helloWord" : "hello world" } }; page(json)
然后我們運行小程序,就可以發(fā)現(xiàn)顯示的就是hello world,即所有的變量都需要包含在注冊界面的data中
有的人可能會問,怎么去動態(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)
都能實現(xiàn)相同效果,每次調(diào)用setData()函數(shù)的是夠都會重新渲染一次頁面。
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">我是測試按鈕</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的作用是對this的作用域的一個擴展。
wx:for 循環(huán)一個變量
wx:for-index 代表循環(huán)的鍵名
wx:for-item 代表循環(huán)的鍵值
users 在頁面顯示的時候動態(tài)的添加到了data作用域中。
現(xiàn)在我們再來看一個新的問題 如上id=”nameDemo” view中{{users[0].name}} 這個值我們怎么去動態(tài)的更改問題
有的可能說直接重新生成一個json直接渲染進去不就行了?
這種方案是可以的,但是要考慮到渲染的性能呀,如果每次調(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對象添加了一個點擊事件,點擊事件對應的函數(shù)是clickFunc 參數(shù)event數(shù)據(jù)結構如下
{ "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 } }
以上是“微信小程序如何實現(xiàn)模版渲染”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標題:微信小程序如何實現(xiàn)模版渲染
鏈接地址:http://www.muchs.cn/article15/phdcdi.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、建站公司、網(wǎng)站策劃、微信公眾號、品牌網(wǎng)站設計、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)