溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2021-07-05 11:04:16 來源:億速云 閱讀:150 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關微信小程序開發(fā)之數(shù)據(jù)存儲、參數(shù)傳遞、數(shù)據(jù)緩存對的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

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

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

先上GIF:

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

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,
 });

頁面間傳遞參數(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 
//獲取應用實例 
var app = getApp() 
Page({ 
 data: { 
 info: app.data.info, 
 infofromindex: '來自index.js的信息', 
 infofrominput: '' 
 }, 
 onLoad: function () { 
 }, 
 //跳轉到新頁面 
 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  bindtap="gotonewpage">跳轉</button> 
<input  placeholder="請輸入需要保存的參數(shù)" bindinput="searchInputEvent" /> 
<button  bindtap="saveinput">存入Storage</button> 
</view>

3.newpage.js

//newpage.js 
//獲取應用實例 
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 >infofromapp:{{infofromapp}}</view> 
<view >infofromindex:{{infofromindex}}</view> 
<view >infofromstorage:{{infofromstorage}}</view>

5.app.js

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

感謝各位的閱讀!關于“微信小程序開發(fā)之數(shù)據(jù)存儲、參數(shù)傳遞、數(shù)據(jù)緩存對的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI