get-battery-info.uvue 617 字节
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 25 26 27 28 29 30 31 32
  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.message);
        uni.showModal({
          content: e.message,
          showCancel: false
        });
      }
    }
  }
33
</script>