get-battery-info.uvue 671 字节
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<template>
  <view>
    <text>当前电量:{{level}}%</text>
    <text>是否充电中:{{isCharging}}</text>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        level: 0,
        isCharging: false
      }
    },
    onLoad() {
      try {
        uni.getBatteryInfo({
          success: res => {
            this.level = res.level;
            this.isCharging = res.isCharging;
          }
        });
      } catch (e) {
        console.error((e as Error).message);
        uni.showModal({
          content: (e as Error).message,
          showCancel: false
        });
      }
    }
  }
33
</script>