有時需要將數據臨時存儲到本地緩存,而不是存儲到數據庫,
1、
<template> <view> <button type="primary" @click="setStor">存儲數據</button> </view> </template> <script> export default { methods: { setStor () { uni.setStorage({ key: 'id', data: 100, success () { console.log('存儲成功') } }) } } } </script> <style> </style>
存儲成功之后,瀏覽器中查看:Application>localstorage,微信開發者工具中查看:storage
2、
<template> <view> <button type="primary" @click="setStor">存儲數據</button> </view> </template> <script> export default { methods: { setStor () { uni.setStorageSync('id',100) } } } </script> <style> </style>
3、uni.getStorage
<template> <view> <button type="primary" @click="getStorage">獲取數據</button> </view> </template> <script> export default { data () { return { id: '' } }, methods: { getStorage () { uni.getStorage({ key: 'id', success: res=>{ console.log("獲取成功",res) } }) } } } </script>
注意:res={data:key對應的內容}
4、
<template> <view> <button type="primary" @click="getStorage">獲取數據</button> </view> </template> <script> export default { methods: { getStorage () { const id = uni.getStorageSync('id') console.log(id) } } } </script>
5、
<template> <view> <button type="primary" @click="removeStorage">刪除數據</button> </view> </template> <script> export default { methods: { removeStorage () { uni.removeStorage({ key: 'id', success: function () { console.log('刪除成功') } }) } } } </script>
6、
<template> <view> <button type="primary" @click="removeStorage">刪除數據</button> </view> </template> <script> export default { methods: { removeStorage () { uni.removeStorageSync('id') } } } </script>