uniapp获取手机网络状态和手机系统信息(如4g,wifi)

比眉伴天荒 2022-12-28 07:39 714阅读 0赞

先看代码,复制使用即可。(uni内置方法uni.getNetworkType用来获取网络状态,uni.getSystemInfo用来获取手机系统)

  1. <template>
  2. <view>
  3. <view @click="getNetworkType">点击获取状态</view>
  4. <view>
  5. 网络状态为:{ { TypeOfNow}}
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. TypeOfNow:'未获取'
  14. };
  15. },
  16. methods:{
  17. getNetworkType() { //获取网络状态
  18. const _this = this ; //改变this指向
  19. uni.getNetworkType({
  20. success: (res) => {
  21. console.log(res)
  22. // res的取值为 {errMsg: "getNetworkType:ok",networkType: "wifi"}
  23. _this.TypeOfNow = res.subtype || res.networkType
  24. },
  25. fail: () => {
  26. uni.showModal({
  27. content:'获取失败!',
  28. showCancel:false
  29. })
  30. }
  31. })
  32. uni.getSystemInfo({ //获取手机系统信息
  33. success: (res) => {
  34. console.log('当前手机系统信息为',res)
  35. }
  36. })
  37. },
  38. }
  39. }
  40. </script>
  41. <style lang="scss">
  42. </style>

其他有关uniapp的疑问或者此方法不理解的地方您可留言,我会尽快回复并帮您解决。

发表评论

表情:
评论列表 (有 0 条评论,714人围观)

还没有评论,来说两句吧...

相关阅读