溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

微信小程序開(kāi)發(fā)中數(shù)據(jù)存儲(chǔ)、參數(shù)傳遞和數(shù)據(jù)緩存的方法

發(fā)布時(shí)間:2022-04-19 15:26:30 來(lái)源:億速云 閱讀:349 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇“微信小程序開(kāi)發(fā)中數(shù)據(jù)存儲(chǔ)、參數(shù)傳遞和數(shù)據(jù)緩存的方法”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“微信小程序開(kāi)發(fā)中數(shù)據(jù)存儲(chǔ)、參數(shù)傳遞和數(shù)據(jù)緩存的方法”文章吧。

先上效果:

微信小程序開(kāi)發(fā)中數(shù)據(jù)存儲(chǔ)、參數(shù)傳遞和數(shù)據(jù)緩存的方法

1.APP.js

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

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

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

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

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

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

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

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

①存儲(chǔ)數(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是需要存儲(chǔ)的內(nèi)容.

詳情見(jiàn)微信小程序開(kāi)發(fā)文檔:文檔

貼上代碼:

1.index.js

//index.js 
//獲取應(yīng)用實(shí)例 
var app = getApp() 
Page({ 
 data: { 
 info: app.data.info, 
 infofromindex: '來(lái)自index.js的信息', 
 infofrominput: '' 
 }, 
 onLoad: function () { 
 }, 
 //跳轉(zhuǎn)到新頁(yè)面 
 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="請(qǐng)輸入需要保存的參數(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: '來(lái)自APP.js的信息' 
 }, 
 onLaunch: function () { 
 
 } 
})

以上就是關(guān)于“微信小程序開(kāi)發(fā)中數(shù)據(jù)存儲(chǔ)、參數(shù)傳遞和數(shù)據(jù)緩存的方法”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI