小程序监听网络状态

174 阅读1分钟

image.png

		onShow: function() {
			this.handleToListenNetworkStatus()
                        },
                 methods: {
			// 监听网络状态
			handleToListenNetworkStatus() {
				uni.getNetworkType({
					success: function (res) {
						if(res.networkType === 'none'){
							uni.showToast({
								title: '无网络服务,请检查网络设置!',
								duration: 3000,
								icon: 'none'
							});
						}
					}
				})
				uni.onNetworkStatusChange(function(res) {
					if (!res.isConnected) {
						uni.showToast({
							title: '无网络服务,请检查网络设置!',
							duration: 5000,
							icon: 'none'
						});
					}
				});
			},

image.png