3***@qq.com
3***@qq.com
  • 发布:2019-02-13 22:50
  • 更新:2022-09-09 11:03
  • 阅读:7300

uni.setStorage不能持久缓存

分类:uni-app

app退出重新登录后,无法获取uni.getStorage之前存储的数据,谢谢

getStorage: function () {  
	var key = 'KEY'  
	if (key.length === 0) {  
		uni.showModal({  
			title: '读取数据失败',  
			content: "key 不能为空",  
			showCancel:false  
		})  
	} else {  
		uni.getStorage({  
			key: key,  
			success: (res) => {  
				uni.showModal({  
					title: '读取数据成功',  
					content: "data: '" + res.data + "'",  
					showCancel:false  
				})  
			},  
			fail: () => {  
				uni.showModal({  
					title: '读取数据失败',  
					content: "找不到 key 对应的数据",  
					showCancel:false  
				})  
			}  
		})  
	}  
},  
setStorage: function () {  
	var key = 'KEY',  
		data = 'abc';  
	if (key.length === 0) {  
		uni.showModal({  
			title: '保存数据失败',  
			content: 'key 不能为空',  
			showCancel:false  
		})  
	} else {  
		uni.setStorage({  
			key: key,  
			data: data,  
			success: (res) => {  
				uni.showModal({  
					title: '存储数据成功',  
					content: ' ',  
					showCancel:false  
				})  
			},  
			fail: () => {  
				uni.showModal({  
					title: '储存数据失败!想',  
					showCancel:false  
				})  
			}  
		})  
	}  
},
2019-02-13 22:50 负责人:无 分享
已邀请:
CaesarHe

CaesarHe - 微信:Caesar-Bosheng-He

自定义基座测试时关闭app后是会清除缓存的,打正式包安装是不会的

  • DCloud_uniCloud_JSON

    除非卸载app或者调用接口设置,是不会自动清除缓存的。自定义基座、标准基座都一样


    2022-03-07 11:00

z***@163.com

z***@163.com

解决了吗 我用这个方法 微信小程序是的 微信后台退出后 数据就没了

  • DCloud_uniCloud_JSON

    小程序是微信清除的,只有app的storage才能持久化存储


    2022-07-08 16:04

萌龙

萌龙

同步的试试看

  • 3***@qq.com (作者)

    同步的也一样,谢谢


    2019-02-14 07:41

  • 3***@qq.com (作者)

    这个问题搞了我好长时间了,怎么都解决不了,这段代码也是直接从官方给的演示里直接拷贝过来的


    2019-02-14 07:42

  • 3***@qq.com (作者)

    官方给的演示app是可以存储,退出重启后也是有数据的


    2019-02-14 07:42

3***@qq.com

3***@qq.com (作者)

在app未退出时,能正常的存储和读取数据,但是退出后再重新登录的话,就获取不到数据了

  • h***@163.com

    你退出登陆的时候把缓存给删除了把,我遇到的问题刚刚好和你的相反,我把进程结束了,重新登陆进来还是能读到缓存


    2019-09-24 17:11

小罗伯特

小罗伯特

碰见了同样的问题,有解决方法吗

  • 冬至D

    现在已经正常的


    2023-01-28 16:19

  • 冬至D

    <template>

    <view class="content">

    <input type="text" maxlength="11" v-model="tel" placeholder="请输入账号">

    <input password type="text" v-model="pwd" placeholder="请输入密码">

    <button @click="login()">登录</button>

    <text>{{tel}}</text>

    <text>{{pwd}}</text>

    </view>

    </template>


    <script>

    export default {

    data() {

    return {

    tel: '',

    pwd:''

    }

    },

    onLoad() {

    var tel = uni.getStorageSync('tel')

    var pwd = uni.getStorageSync('pwd')

    if(tel){

    this.tel = tel

    this.pwd = pwd

    }

    },

    methods: {

    login(){

    uni.setStorageSync('tel',this.tel)

    uni.setStorageSync('pwd',this.pwd)

    }

    }

    }

    </script>


    <style>

    .content {

    display: flex;

    flex-direction: column;

    align-items: center;

    justify-content: center;

    }

    </style>


    2023-01-28 16:19

要回复问题请先登录注册