When using uniapp to develop an app, there are several functions, such as obtaining the cache information of the application and clearing the cache information, as well as obtaining the version number of the current software. At this time, you need to use the method of the app, as follows:
1. Get application cache
plus.cache.calculate
How to use- Define a variable in data,
- Encapsulate a method or get it in the onLoad life cycle
//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. Get the application version number
plus.runtime.getProperty
How to use- Define a variable in data,
version
- Encapsulate a method or get it in the onLoad life cycle
//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. Prohibit APP horizontal screen
Sometimes when using the app, if the phone turns on the auto-rotate screen, turning the phone will automatically display the horizontal screen, causing the interface style to be inconsistent with expectations. At this time, plus.screen.lockOrientation
the method can be used to solve it, as follows:
- Find the project root directory
App.vue
onLaunch
Add the following code in the life cycle
onLaunch: function() {
plus.screen.lockOrientation("portrait-primary");// 禁止转动屏幕
}