get-battery-info.uvue 639 B

123456789101112131415161718192021222324252627282930313233
  1. <template>
  2. <view>
  3. <text>当前电量:{{level}}%</text>
  4. <text>是否充电中:{{isCharging}}</text>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. level: 0,
  12. isCharging: false
  13. }
  14. },
  15. onLoad() {
  16. try {
  17. uni.getBatteryInfo({
  18. success: res => {
  19. this.level = res.level;
  20. this.isCharging = res.isCharging;
  21. }
  22. });
  23. } catch (e) {
  24. console.error((e as Error).message);
  25. uni.showModal({
  26. content: (e as Error).message,
  27. showCancel: false
  28. });
  29. }
  30. }
  31. }
  32. </script>