這篇“微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用的方法”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用的方法”文章吧。
微信小程序 詳解Page中data數(shù)據(jù)操作和函數(shù)調(diào)用
Page() 函數(shù)用來(lái)注冊(cè)一個(gè)頁(yè)面。接受一個(gè) object 參數(shù),其指定頁(yè)面的初始數(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 無(wú)效,無(wú)法改變頁(yè)面的狀態(tài),還會(huì)造成數(shù)據(jù)不一致。<br> (2)、單次設(shè)置的數(shù)據(jù)不能超過(guò)1024kB,請(qǐng)盡量避免一次設(shè)置過(guò)多的數(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>
以上就是關(guān)于“微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用的方法”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用的方法-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://www.rwnh.cn/article2/ddopic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、軟件開(kāi)發(fā)、外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容