鸿蒙HarmonyOS获取当前App版本号,话不多说直接上代码
import bundleManager from '@ohos.bundle.bundleManager';
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct VersionExample {
@State version: string = '';
aboutToAppear() {
this.getAppVersion();
}
// 获取应用版本号
async getAppVersion() {
try {
const bundleInfo = await bundleManager.getBundleInfoForSelf(0);
console.log('----版本号', bundleInfo.versionName)
this.version = bundleInfo.versionName; // 获取版本名称
} catch (error) {
console.error('获取版本号失败:', (error as BusinessError).message);
}
}
build() {
Column() {
Text(`当前App应用版本号: ${this.version}`)
.fontSize(20)
.margin({ top: 20 })
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
.backgroundColor('#F0F0F0')
}
}