简体中文
获取网络类型
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.9 | 4.11 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | GetNetworkTypeOptions | 是 | - | - | |||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| networkType | string | 是 | - | 网络类型 | |
| hasSystemProxy | boolean | 否 | - | 需要基础库: 2.22.1设备是否使用了网络代理 | |
| signalStrength | number | 否 | - | 信号强弱,单位 dbm | |
| weakNet | boolean | 否 | - | 需要基础库: 3.5.3是否处于弱网环境 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errSubject | string | 是 | - | - | 统一错误主题(模块)名称 |
| errCode | number | 是 | - | - | 统一错误码 |
| errMsg | string | 是 | - | - | 统一错误描述信息 |
| data | any | 否 | - | - | 错误信息中包含的数据 |
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError |
| name | string | 是 | - | - | |
| message | string | 是 | - | - |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例
<template>
<page-head :title="data.title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-container">
<view class="uni-center">网络状态</view>
<view v-if="data.hasNetworkType == false">
<view class="uni-center uni-common-mt">未获取</view>
<view class="uni-center uni-common-mt">请点击下面按钮获取网络状态</view>
</view>
<view v-if="data.hasNetworkType == true">
<view class="uni-center uni-common-mt">{{data.networkType}}</view>
</view>
</view>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="getNetworkType">获取设备网络状态</button>
<button class="uni-common-mt" @tap="clear">清空</button>
</view>
</view>
</template>
<script setup lang="uts">
type DataType = {
title: string;
hasNetworkType: boolean;
networkType: string;
connectedWifi: string;
jest_result: boolean;
}
const data = reactive({
title: 'getNetworkType',
hasNetworkType: false,
networkType: '',
connectedWifi: '',
//自动化测试例专用
jest_result: false,
} as DataType)
onUnload(() => {
data.networkType = '';
data.hasNetworkType = false;
})
const getNetworkType = () => {
uni.getNetworkType({
success: (res) => {
console.log(res)
data.hasNetworkType = true;
data.networkType = res.networkType
},
fail: () => {
uni.showModal({
content: '获取失败!',
showCancel: false
})
}
})
}
const clear = () => {
data.hasNetworkType = false;
data.networkType = '';
data.connectedWifi = '';
}
//自动化测试例专用
const jest_getNetworkType = () => {
uni.getNetworkType({
success: () => {
data.jest_result = true;
},
fail: () => {
data.jest_result = false;
}
})
}
defineExpose({
data,
jest_getNetworkType
})
</script>
<style>
</style>
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | 错误信息 |