溫馨提示×

溫馨提示×

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

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

微信小程序數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存對的方法

發(fā)布時間:2022-04-13 14:13:30 來源:億速云 閱讀:536 作者:iii 欄目:編程語言

這篇文章主要介紹“微信小程序數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存對的方法”,在日常操作中,相信很多人在微信小程序數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存對的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”微信小程序數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存對的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

先上GIF:

微信小程序數(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 
//獲取應(yīng)用實例 
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  bindtap="gotonewpage">跳轉(zhuǎn)</button> 
<input  placeholder="請輸入需要保存的參數(shù)" bindinput="searchInputEvent" /> 
<button  bindtap="saveinput">存入Storage</button> 
</view>

3.newpage.js

//newpage.js 
//獲取應(yīng)用實例 
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 () { 
 
 } 
})

到此,關(guān)于“微信小程序數(shù)據(jù)存儲、參數(shù)傳遞和數(shù)據(jù)緩存對的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

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

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

AI