uni开发app获取应用缓存,获取应用版本号,禁止屏幕转动

使用uniapp开发app的时候,有这么几个功能,获取应用的缓存信息和清除缓存信息,以及获取当前软件的版本号,这时就需要使用app的方法,如下:

1.获取应用缓存

  1. 使用plus.cache.calculate方法
  2. 在data定义一个变量,
  3. 封装一个方法或在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.获取应用版本号

  1. 使用plus.runtime.getProperty方法
  2. 在data定义一个变量,version
  3. 封装一个方法或在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方法就可以解决,如下:

  1. 找到项目根目录下的App.vue
  2. onLaunch生命周期中添加如下代码
onLaunch: function() {
    
    
	plus.screen.lockOrientation("portrait-primary");// 禁止转动屏幕
}

猜你喜欢

转载自blog.csdn.net/qq_38188228/article/details/126776220
今日推荐