微信小程序開發(fā)之?dāng)?shù)據(jù)存儲 參數(shù)傳遞 數(shù)據(jù)緩存
微信小程序開發(fā)內(nèi)測一個月.數(shù)據(jù)傳遞的方式很少.經(jīng)常遇到頁面銷毀后回傳參數(shù)的問題,小程序中并沒有類似Android的startActivityForResult的方法,也沒有類似廣播這樣的通訊方式,更沒有類似eventbus的輪子可用.
現(xiàn)在已知傳遞參數(shù)的方法只找到三種,先總結(jié)下.由于正處于內(nèi)測階段,文檔也不是很穩(wěn)定,經(jīng)常修改,目前尚沒有人造輪子.
先上GIF:
1.APP.js
我把常用且不會更改的參數(shù)放在APP.js的data里面了.在各個page中都可以拿到var app = getApp();
app上就可以拿到存在data中的參數(shù).
2. wx.navigateTo({})中URL攜帶參數(shù)
demo中已經(jīng)寫出:
wx.navigateTo({ url: "../newpage/newpage?infofromindex=" + this.data.infofromindex, });
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 () { } })
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- 微信小程序 動態(tài)修改頁面數(shù)據(jù)及參數(shù)傳遞過程詳解
- 微信小程序?qū)崿F(xiàn)頁面跳轉(zhuǎn)傳遞參數(shù)(實(shí)體,對象)
- 微信小程序?qū)崿F(xiàn)傳遞多個參數(shù)與事件處理
- 微信小程序?qū)W習(xí)筆記之跳轉(zhuǎn)頁面、傳遞參數(shù)獲得數(shù)據(jù)操作圖文詳解
- 微信小程序之頁面跳轉(zhuǎn)和參數(shù)傳遞的實(shí)現(xiàn)
- 微信小程序教程系列之頁面跳轉(zhuǎn)和參數(shù)傳遞(6)
- 微信小程序 navigator 跳轉(zhuǎn)url傳遞參數(shù)
- 微信小程序之間的參數(shù)傳遞、獲取的操作方法
相關(guān)文章
w3c編程挑戰(zhàn)_初級腳本算法實(shí)戰(zhàn)篇
下面小編就為大家?guī)硪黄獁3c編程挑戰(zhàn)_初級腳本算法實(shí)戰(zhàn)篇。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06js與jquery獲取父級元素,子級元素,兄弟元素的實(shí)現(xiàn)方法
本篇文章主要是對js與jquery獲取父級元素,子級元素,兄弟元素的實(shí)現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01用JavaScript和jQuery實(shí)現(xiàn)瀑布流
本篇文章主要介紹了用JavaScript和jQuery實(shí)現(xiàn)瀑布流的方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03Javascript實(shí)現(xiàn)一朵從含苞到綻放的玫瑰
今天小編就為大家分享一篇關(guān)于Javascript實(shí)現(xiàn)一朵從含苞到綻放的玫瑰,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03JS一分鐘在github+Jekyll的博客中添加訪問量功能的實(shí)現(xiàn)
這篇文章主要介紹了JS一分鐘在github+Jekyll的博客中添加訪問量功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04JavaScript實(shí)現(xiàn)的瀏覽器下載文件的方法
本文通過一段簡單的代碼給大家介紹了js實(shí)現(xiàn)瀏覽器下載文件的方法,需要的的朋友參考下吧2017-08-08