微信小程序開發(fā)中數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹了微信小程序開發(fā)中數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

你所需要的網(wǎng)站建設(shè)服務(wù),我們均能行業(yè)靠前的水平為你提供.標(biāo)準(zhǔn)是產(chǎn)品質(zhì)量的保證,主要從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站建設(shè)、成都手機(jī)網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、成都品牌網(wǎng)站建設(shè)、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。創(chuàng)新互聯(lián)擁有實(shí)力堅(jiān)強(qiáng)的技術(shù)研發(fā)團(tuán)隊(duì)及素養(yǎng)的視覺設(shè)計(jì)專才。

微信小程序開發(fā)內(nèi)測一個(gè)月.數(shù)據(jù)傳遞的方式很少.經(jīng)常遇到頁面銷毀后回傳參數(shù)的問題,小程序中并沒有類似Android的startActivityForResult的方法,也沒有類似廣播這樣的通訊方式,更沒有類似eventbus的輪子可用.

現(xiàn)在已知傳遞參數(shù)的方法只找到三種,先總結(jié)下.由于正處于內(nèi)測階段,文檔也不是很穩(wěn)定,經(jīng)常修改,目前尚沒有人造輪子.

先上GIF:

微信小程序開發(fā)中數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存的示例分析

1.APP.js

我把常用且不會更改的參數(shù)放在APP.js的data里面了.在各個(gè)page中都可以拿到var app = getApp();

app上就可以拿到存在data中的參數(shù).

2. wx.navigateTo({})中URL攜帶參數(shù)

demo中已經(jīng)寫出:

 wx.navigateTo({
 url: "../newpage/newpage?infofromindex=" + this.data.infofromindex,
 });

頁面間傳遞參數(shù)的筆記

3.wx.setStorage(OBJECT) 數(shù)據(jù)緩存

微信開發(fā)文檔中的數(shù)據(jù)緩存方法:

①存儲數(shù)據(jù)

 try {
 wx.setStorageSync('infofrominput', this.data.infofrominput)
 } catch (e) {
 }

②獲取數(shù)據(jù)

 //獲取
 wx.getStorage({
  key: 'infofrominput',
  success: function (res) {
  _this.setData({
   infofromstorage: res.data,
  })
  }
 })

key是本地緩存中的指定的 key,data是需要存儲的內(nèi)容.

詳情見微信小程序開發(fā)文檔:文檔

貼上代碼:

1.index.js

//index.js 
//獲取應(yīng)用實(shí)例 
var app = getApp() 
Page({ 
 data: { 
 info: app.data.info, 
 infofromindex: '來自index.js的信息', 
 infofrominput: '' 
 }, 
 onLoad: function () { 
 }, 
 //跳轉(zhuǎn)到新頁面 
 gotonewpage: function () { 
 wx.navigateTo({ 
 url: "../newpage/newpage?infofromindex=" + this.data.infofromindex, 
 }); 
 }, 
 //獲取輸入值 
 searchInputEvent: function (e) { 
 console.log(e.detail.value) 
 this.setData({ infofrominput: e.detail.value }) 
 }, 
 //保存參數(shù) 
 saveinput: function () { 
 try { 
 wx.setStorageSync('infofrominput', this.data.infofrominput) 
 } catch (e) { 
 } 
 } 
})

2.index.wxml

<!--index.wxml--> 
<view> 
<button style="background-color:#00ff00;margin:20rpx" bindtap="gotonewpage">跳轉(zhuǎn)</button> 
<input style="background-color:#eee;margin:20rpx;height:80rpx" placeholder="請輸入需要保存的參數(shù)" bindinput="searchInputEvent" /> 
<button style="background-color:#ff0000;margin:20rpx" bindtap="saveinput">存入Storage</button> 
</view>

3.newpage.js

//newpage.js 
//獲取應(yīng)用實(shí)例 
var app = getApp() 
Page({ 
 data: { 
 infofromapp: app.data.infofromapp, 
 infofromindex: '', 
 infofromstorage: '', 
 }, 
 onLoad: function (options) { 
 var _this = this; 
 var infofromindex = options.infofromindex; 
 this.setData({ 
  infofromindex: infofromindex 
 }) 
 //獲取 
 wx.getStorage({ 
  key: 'infofrominput', 
  success: function (res) { 
  _this.setData({ 
   infofromstorage: res.data, 
  }) 
  } 
 }) 
 } 
})

4.newpage.wxml

<!--newpage.wxml--> 
<view style="width:100%;margin:30rpx">infofromapp:{{infofromapp}}</view> 
<view style="width:100%;margin:30rpx">infofromindex:{{infofromindex}}</view> 
<view style="width:100%;margin:30rpx">infofromstorage:{{infofromstorage}}</view>

5.app.js

//app.js 
App({ 
 data: { 
 infofromapp: '來自APP.js的信息' 
 }, 
 onLaunch: function () { 
 
 } 
})

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“微信小程序開發(fā)中數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存的示例分析”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)站題目:微信小程序開發(fā)中數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存的示例分析-創(chuàng)新互聯(lián)
瀏覽地址:http://www.muchs.cn/article36/ipcpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站制作、域名注冊定制網(wǎng)站、微信小程序、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名