uniapp清理快取
阿新 • • 發佈:2022-03-08
<template> <view class="content"> <view>應用快取:{{storageSize}}</view> <button type="default" @tap="clearStorage">清除快取</button> </view> </template> <script> export default { data() { return { storageSize :'' } }, onLoad() { this.getStorageSize(); }, methods:{ getStorageSize:function(){ let that = this; uni.getStorageInfo({ success(res) { console.log(res) console.log(res.keys); console.log(res.limitSize); let size= res.currentSize; if (size < 1024) { that.storageSize = size + ' B'; } else if (size/1024>=1 && size/1024/1024<1) { that.storageSize = Math.floor(size/1024*100)/100 + ' KB'; } else if (size/1024/1024>=1) { that.storageSize = Math.floor(size/1024/1024*100)/100 + ' M'; } } }) }, clearStorage:function (){ let that = this; uni.showModal({ title:'提示', content:'確定清除快取嗎?', confirmText:'立即清除', success(res) { if(res.confirm){ uni.clearStorageSync(); //重新獲取並顯示清除後的快取大小 that.getStorageSize(); uni.showToast({ title:'清除成功' }) } } }) } } } </script>