小程序临时存储
Ⅰ 微信小程序怎么进行数据缓存
在微信小程序中,数据缓存其实就和localstorage 的原理差不多,所以理解起来并不难。下面我们来一起实现一下。
效果图展示:
我们在index页面存入数字11,然后在跳转到新页面,在将缓存中的11取出渲染到当前页面。具体代码如下:
index页面:
<span style="font-size:24px;">
<view class="btn-area">
<navigator url="../navigator/navigator?title=我是navi">跳转到新的页面post情求</navigator>
<navigator url="../redirect/redirect?title=我是red" redirect>跳转到当前页面</navigator>
</view>
</span>
<view>
<input style="border:2rpx solid red" placeholder="输入信息" bindinput="getInput" />
<button style="border:2rpx solid yellow" bindtap="saveInput">存入</button>
</view>1234567891012345678910
index的js:
//index.js
//获取应用实例
var app = getApp()
Page({
data: {
storage:''
},
onLoad: function () {
var that = this
//获取输入值
getInput:function(e){
this.setData({
storage:e.detail.value
})
},
//存储输入值
saveInput:function(){
wx.setStorageSync('storage', this.data.storage)
}
})
2223
跳转页面:
<view>从存储中得到的数据:{{storage}}</view>11
跳转页面的js:
var app = getApp();
var that;
Page( {
data: {
storage:''
},
onLoad: function(options) {
that = this;
//获取存储信息
wx.getStorage({
key: 'storage',
success: function(res){
// success
that.setData({
storage:res.data
})
}
})
}
})
Ⅱ 微信小程序怎么进行数据缓存
每个微信小程序都可以有自己的本地缓存,可以通过 wx.setStorag(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)可以对本地缓存进行设置、获取和清理。本地缓存最大为10MB。
注意:
localStorage 是永久存储的,但是我们不建议将关键信息全部存在 localStorage,以防用户换设备的情况。
wx.setStorage(OBJECT)将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。
代码示例
wx.setStorage({ key:"key" data:"value" })1234
wx.setStorageSync(KEY,DATA)
将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。
try { wx.setStorageSync('key', 'value') } catch (e) { }1234
wx.getStorageInfo(OBJECT)
异步获取当前storage的相关信息
wx.getStorageInfo({ success: function(res) { console.log(res.keys) console.log(res.currentSize) console.log(res.limitSize) } })1234567
wx.getStorageInfoSync
同步获取当前storage的相关信息
try { var res = wx.getStorageInfoSync() console.log(res.keys) console.log(res.currentSize) console.log(res.limitSize) } catch (e) { // Do something when catch error }12345678
wx.removeStorage(OBJECT)
从本地缓存中异步移除指定 key 。
wx.removeStorage({ key: 'key', success: function(res) { console.log(res.data) } })123456
wx.removeStorageSync(KEY)
从本地缓存中同步移除指定 key 。 try { wx.removeStorageSync('key') } catch (e) { // Do something when catch error }123456
wx.clearStorage()
清理本地数据缓存。
wx.clearStorage()1
wx.clearStorageSync()
同步清理本地数据缓存
try { wx.clearStorageSync() } catch(e) { // Do something when catch error }
Ⅲ 微信小程序怎么保存
微信小程序不用保存,只要你使用过一次,就会存在小程序处,下次打开微信后,下拉顶部页面就会弹出使用过的小程序。