on-network-status-change.js 639 B

1234567891011121314151617181920212223242526272829303132
  1. Page({
  2. onShareAppMessage() {
  3. return {
  4. title: '监听手机网络变化',
  5. path: 'page/API/pages/on-network-status-change/on-network-status-change'
  6. }
  7. },
  8. data: {
  9. isConnected: false,
  10. },
  11. onLoad() {
  12. const that = this
  13. wx.onNetworkStatusChange(function (res) {
  14. that.setData({
  15. isConnected: res.isConnected,
  16. networkType: res.networkType
  17. })
  18. })
  19. },
  20. onShow() {
  21. const that = this
  22. wx.getNetworkType({
  23. success(res) {
  24. that.setData({
  25. isConnected: res.networkType !== 'none',
  26. networkType: res.networkType
  27. })
  28. }
  29. })
  30. }
  31. })