微信小程序 詳解Page中data數(shù)據(jù)操作和函數(shù)調(diào)用
更新時(shí)間:2017年01月12日 17:06:05 投稿:lqh
這篇文章主要介紹了微信小程序 詳解Page中data數(shù)據(jù)操作和函數(shù)調(diào)用的相關(guān)資料,需要的朋友可以參考下
微信小程序 詳解Page中data數(shù)據(jù)操作和函數(shù)調(diào)用
Page() 函數(shù)用來注冊(cè)一個(gè)頁面。接受一個(gè) object 參數(shù),其指定頁面的初始數(shù)據(jù)、生命周期函數(shù)、事件處理函數(shù)等。
//index.js
<pre code_snippet_id="2049407" snippet_file_name="blog_20161214_1_1145312" name="code" class="javascript">Page({
data: {
text: "This is page data.",</pre><pre code_snippet_id="2049407" snippet_file_name="blog_20161214_2_861121" name="code" class="javascript"> sliderOffset: 0,
sliderLeft: 0,
state:{
genre:[],
genre_index: 0,
model:[],
model_index: 0,
terminalStatus:'',
}
},
onLoad: function(options) {
// Do some initialize when page load.
},
onReady: function() {
// Do something when page ready.
},
onShow: function() {
// Do something when page show.
},
onHide: function() {
// Do something when page hide.
},
onUnload: function() {
// Do something when page close.
},
onPullDownRefresh: function() {
// Do something when pull down.
},
onReachBottom: function() {
// Do something when page reach bottom.
},
// Event handler.
viewTap: function () {
var p = this.position
ball(p, 150)
function ball(x, y) {
console.log(x,y)
}
},
customData: {
hi: 'MINA'
}
})</pre><br>
<p></p>
<pre></pre>
<br>
1、設(shè)置data數(shù)據(jù)
<p></p>
<p><span style="font-size:14px">setData 函數(shù)用于將數(shù)據(jù)從邏輯層發(fā)送到視圖層,同時(shí)改變對(duì)應(yīng)的 this.data 的值。<br>
注意:<br>
(1)、直接修改 this.data 無效,無法改變頁面的狀態(tài),還會(huì)造成數(shù)據(jù)不一致。<br>
(2)、單次設(shè)置的數(shù)據(jù)不能超過1024kB,請(qǐng)盡量避免一次設(shè)置過多的數(shù)據(jù)。<br>
</span></p>
<p><span style="font-size:14px">setData() 參數(shù)格式:接受一個(gè)對(duì)象,以 key,value 的形式表示將 this.data 中的 key 對(duì)應(yīng)的值改變成 value。其中 key 可以非常靈活,以數(shù)據(jù)路徑的形式給出,如 array[2].message,a.b.c.d,并且不需要在 this.data 中預(yù)先定義。<br>
</span></p>
<p><span style="font-size:14px">下面設(shè)置data中的text和genre_index的值</span></p>
<p><span style="font-size:14px"></span></p><pre code_snippet_id="2049407" snippet_file_name="blog_20161214_3_1831450" name="code" class="html">this.setData({
'state.genre_index':1,
text:'data value'
})</pre><p></p>
<p>2、獲取data數(shù)據(jù)</p>
<p>獲取data中的text和genre_index值需要使用this</p>
<p></p><pre code_snippet_id="2049407" snippet_file_name="blog_20161214_4_5833420" name="code" class="javascript"><pre code_snippet_id="2049407" snippet_file_name="blog_20161214_4_5833420" name="code" class="javascript">var gener_index=this.data.state.genre_index</pre><pre code_snippet_id="2049407" snippet_file_name="blog_20161214_5_8406932" name="code" class="javascript">var text=this.data.text</pre><p></p>
<pre></pre>
3、調(diào)用viewTap函數(shù)
<p></p>
<p>在viewTap函數(shù)中調(diào)用內(nèi)部的ball函數(shù)可以直接調(diào)用,如果需要在onReady函數(shù)中調(diào)用viewTap函數(shù)需要使用this。</p>
<p></p><pre code_snippet_id="2049407" snippet_file_name="blog_20161214_6_6869005" name="code" class="html">onReady: function () {
this.drawBall()
},</pre><br>
<br>
<br>
<p></p>
<br>
</pre>
您可能感興趣的文章:
- 微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用方法
- 詳解微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用
- 詳解如何使用微信小程序云函數(shù)發(fā)送短信驗(yàn)證碼
- 詳解在微信小程序的JS腳本中使用Promise來優(yōu)化函數(shù)處理
- 微信小程序常用簡(jiǎn)易小函數(shù)總結(jié)
- 微信小程序提取公用函數(shù)到util.js及使用方法示例
- 解決mpvue + vuex 開發(fā)微信小程序vuex輔助函數(shù)mapState、mapGetters不可用問題
- 微信小程序 功能函數(shù)小結(jié)(手機(jī)號(hào)驗(yàn)證*、密碼驗(yàn)證*、獲取驗(yàn)證碼*)
- 微信小程序 定義全局?jǐn)?shù)據(jù)、函數(shù)復(fù)用、模版等詳細(xì)介紹
- 詳解微信小程序的不同函數(shù)調(diào)用的幾種方法
相關(guān)文章
JS實(shí)現(xiàn)可恢復(fù)的文件上傳示例詳解
這篇文章主要為大家介紹了JS實(shí)現(xiàn)可恢復(fù)的文件上傳示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
微信小程序 高德地圖SDK詳解及簡(jiǎn)單實(shí)例(源碼下載)
這篇文章主要介紹了微信小程序 高德地圖詳解及簡(jiǎn)單實(shí)例(源碼下載)的相關(guān)資料,需要的朋友可以參考下2017-01-01
JS前端使用canvas實(shí)現(xiàn)擴(kuò)展物體類和事件派發(fā)
這篇文章主要為大家介紹了JS前端使用canvas實(shí)現(xiàn)擴(kuò)展物體類和事件派發(fā)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
JS輕量級(jí)函數(shù)式編程實(shí)現(xiàn)XDM一
這篇文章主要為大家介紹了JS輕量級(jí)函數(shù)式編程實(shí)現(xiàn)XDM示例詳解第1/3篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

