(fix)修复电池电量显示小数点故障

This commit is contained in:
wangchangqi 2023-01-13 16:31:09 +08:00
parent 994d374615
commit ae66a39b83
1 changed files with 6 additions and 3 deletions

View File

@ -19,6 +19,10 @@ class BatteryWidget extends GaiaWidget {
@state() percents = 100
getBatteryPercents() {
return Number((this.batteryManager.level * 100).toPrecision(2))
}
batteryManager!: BatteryManager
constructor({
@ -51,8 +55,7 @@ class BatteryWidget extends GaiaWidget {
init = async () => {
this.batteryManager = await navigator.getBattery()
this.percents = this.batteryManager.level * 100
this.percents = this.getBatteryPercents()
this.charge = this.batteryManager.charging
this.lifeCycle = 'initialized'
@ -67,7 +70,7 @@ class BatteryWidget extends GaiaWidget {
handleEvent(event: Event): void {
switch (event.type) {
case 'levelchange':
this.percents = this.batteryManager.level * 100
this.percents = this.getBatteryPercents()
break
case 'chargingchange':
this.charge = this.batteryManager.charging