使用uniapp开发app的时候,有这么几个功能,获取应用的缓存信息和清除缓存信息,以及获取当前软件的版本号,这时就需要使用app的方法,如下:
1.获取应用缓存
- 使用
plus.cache.calculate
方法 - 在data定义一个变量,
- 封装一个方法或在onLoad生命周期中获取
//size表示多少个字节,单位是b
data(){
return {
cache: '0KB',
}
},
onLoad() {
// #ifdef APP-PLUS
// size表示多少个字节,单位是b
plus.cache.calculate((size) => {
// 判断属于那个区间
if (size < 1024) {
this.cache = size + 'B';
} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
this.cache = Math.floor(size / 1024 * 100) / 100 + 'KB';
} else if (size / 1024 / 1024 >= 1) {
this.cache = Math.floor(size / 1024 / 1024 * 100) / 100 + 'M';
}
});
// #endif
}
2.获取应用版本号
- 使用
plus.runtime.getProperty
方法 - 在data定义一个变量,
version
- 封装一个方法或在onLoad生命周期中获取
//size表示多少个字节,单位是b
data(){
return {
version : '',
}
},
onLoad() {
// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
console.log(wgtinfo.version); //应用版本号
this.version = wgtinfo.version
});
// #endif
}
3.禁止APP横屏
有时候在使用app的时候,如果手机打开了自动旋转屏幕,转动手机就会出现自动横屏,导致界面样式和预期不一致。此时使用plus.screen.lockOrientation
方法就可以解决,如下:
- 找到项目根目录下的
App.vue
- 在
onLaunch
生命周期中添加如下代码
onLaunch: function() {
plus.screen.lockOrientation("portrait-primary");// 禁止转动屏幕
}