您好,登錄后才能下訂單哦!
這篇文章主要介紹“微信小程序Page中data數(shù)據(jù)操作和函數(shù)怎么調(diào)用”,在日常操作中,相信很多人在微信小程序Page中data數(shù)據(jù)操作和函數(shù)怎么調(diào)用問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”微信小程序Page中data數(shù)據(jù)操作和函數(shù)怎么調(diào)用”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
微信小程序 詳解Page中data數(shù)據(jù)操作和函數(shù)調(diào)用
Page() 函數(shù)用來注冊(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 >setData 函數(shù)用于將數(shù)據(jù)從邏輯層發(fā)送到視圖層,同時(shí)改變對(duì)應(yīng)的 this.data 的值。<br> 注意:<br> (1)、直接修改 this.data 無效,無法改變頁(yè)面的狀態(tài),還會(huì)造成數(shù)據(jù)不一致。<br> (2)、單次設(shè)置的數(shù)據(jù)不能超過1024kB,請(qǐng)盡量避免一次設(shè)置過多的數(shù)據(jù)。<br> </span></p> <p><span >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 >下面設(shè)置data中的text和genre_index的值</span></p> <p><span ></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)用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(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)容。