get-battery-info.uvue 639 字节
Newer Older
1
<template>
2 3 4 5
  <view>
    <text>当前电量:{{level}}%</text>
    <text>是否充电中:{{isCharging}}</text>
  </view>
6 7 8
</template>

<script>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  export default {
    data() {
      return {
        level: 0,
        isCharging: false
      }
    },
    onLoad() {
      try {
        uni.getBatteryInfo({
          success: res => {
            this.level = res.level;
            this.isCharging = res.isCharging;
          }
        });
      } catch (e) {
雪洛's avatar
雪洛 已提交
25
        console.error((e as Error).message);
26
        uni.showModal({
雪洛's avatar
雪洛 已提交
27
          content: (e as Error).message,
28 29 30 31 32
          showCancel: false
        });
      }
    }
  }
33
</script>