uniapp清理页面缓存
时间: 2023-10-01 08:09:05 浏览: 286
要清理uniapp页面缓存,可以按照以下步骤进行操作:
1. 在HTML部分,添加一个触发清理缓存操作的按钮:
```html
<view class="li" @click="clearStorage">清理缓存 <text class="current-size">{{currentSize}}</text> <uni-icons type="arrowright" size="18" color="#ddd" class="fr"></uni-icons></view>
```
2. 在script的js部分,定义清理缓存相关的方法和获取缓存大小的方法:
```javascript
data() {
return {
storageSize: '0M',
}
},
onLoad() {
this.getStorageSize() //获取缓存
},
methods: {
//获取app的缓存大小
getStorageSize: function () {
let that = this;
uni.getStorageInfo({
success(res) {
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: '清除成功',
icon: 'none'
})
//清除完后跳到登录页面
setTimeout(() => {
uni.redirectTo({
url: '/pages/login/login',
animationType: 'pop-in',
animationDuration: 200
})
}, 1300)
}
}
})
},
}
```
3. 在组件里添加一个触发清理缓存的事件:
```html
<view class="row" @click="clearStorage">
<view class="title">清除缓存</view>
<view class="right">
<view class="tis tell">{{storageSize}}</view>
<uni-icons type="arrowright" color="#C9C9C9" size="20"></uni-icons>
</view>
</view>
```
阅读全文