The uni development app obtains the application cache, obtains the application version number, and prohibits the screen from rotating

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

  1. plus.cache.calculateHow to use
  2. Define a variable in data,
  3. 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

  1. plus.runtime.getPropertyHow to use
  2. Define a variable in data,version
  3. 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.lockOrientationthe method can be used to solve it, as follows:

  1. Find the project root directoryApp.vue
  2. onLaunchAdd the following code in the life cycle
onLaunch: function() {
    
    
	plus.screen.lockOrientation("portrait-primary");// 禁止转动屏幕
}

Guess you like

Origin blog.csdn.net/qq_38188228/article/details/126776220