16.数据缓存

数据缓存

1.uni.setStorage

官方文档

将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。

代码演示



缓存代码:

setStor () {

uni.setStorage({

key: 'id',

data: 100,

success () {

console.log('存储成功')

}

})

}


2.uni.setStorageSync

将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。

代码演示


setStor () {

uni.setStorageSync('id',100)

}


3.uni.getStorage

从本地缓存中异步获取指定 key 对应的内容。

代码演示



代码:

uni.getStorage({

key: 'id',

success:  res=>{

this.id = res.data

}

})

}

}



4.uni.getStorageSync

从本地缓存中同步获取指定 key 对应的内容。

代码演示

代码:

getStorage () {

const id = uni.getStorageSync('id')

console.log(id)

}


5.uni.removeStorage

从本地缓存中异步移除指定 key。

代码演示


代码:

removeStorage () {

uni.removeStorage({

key: 'id',

success: function () {

console.log('删除成功')

}

})

}

}

6.uni.removeStorageSync

从本地缓存中同步移除指定 key。

代码演示



代码:

removeStorage () {

uni.removeStorageSync('id')

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容