useDeviceInfo — Device Info
17:43Description
useDeviceInfo collects the brand, model, platform and system fields from uni.getDeviceInfo() and is read-only at runtime. Device info is collected once at module level, and every caller shares the same data.
Examples
Device identity
Reads deviceBrand, deviceModel and deviceType, falling back to - with $u.strOr when a field is an empty string.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in deviceRows"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
/>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u, useDeviceInfo } from '@/uni_modules/uview-pro-x';
// 解构方式使用:const { deviceBrand } = useDeviceInfo()
const { info } = useDeviceInfo();
type Row = {
desc: string;
result: string;
last: boolean;
};
// 部分平台字段为空串,展示层统一用 $u.strOr 回退
const deviceRows = computed((): Row[] => {
const current = info.value;
return [
{ desc: 'deviceBrand', result: $u.strOr(current.deviceBrand, '-'), last: false },
{ desc: 'deviceModel', result: $u.strOr(current.deviceModel, '-'), last: false },
{ desc: 'deviceType', result: $u.strOr(current.deviceType, '-'), last: true }
];
});
</script>System and platform
Reads platform, osName, osVersion and system, with the same empty-string fallback applied in the view layer.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in systemRows"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
/>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u, useDeviceInfo } from '@/uni_modules/uview-pro-x';
const { info } = useDeviceInfo();
type Row = {
desc: string;
result: string;
last: boolean;
};
const systemRows = computed((): Row[] => {
const current = info.value;
return [
{ desc: 'platform', result: $u.strOr(current.platform, '-'), last: false },
{ desc: 'osName', result: $u.strOr(current.osName, '-'), last: false },
{ desc: 'osVersion', result: $u.strOr(current.osVersion, '-'), last: false },
{ desc: 'system', result: $u.strOr(current.system, '-'), last: true }
];
});
</script>Screen orientation
Reads the deviceOrientation snapshot, which does not update automatically when the device is rotated after the page is created.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in orientationRows"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
/>
</ux-cell-group>
<ux-gap :height="12" />
<ux-alert-tips type="warning" :show-icon="true" title="deviceOrientation 在页面创建时采集一次,横竖屏切换不会自动更新。" />
</template>
<script setup lang="ts">
import { $u, useDeviceInfo } from '@/uni_modules/uview-pro-x';
const { info } = useDeviceInfo();
type Row = {
desc: string;
result: string;
last: boolean;
};
const orientationRows = computed((): Row[] => [
{ desc: 'deviceOrientation', result: $u.strOr(info.value.deviceOrientation, '-'), last: true }
]);
</script>Usage
Explains that device info never changes at runtime, some fields are empty strings on H5, and orientation changes should be detected through the window width and height from useWindowInfo.
View source
vue
<template>
<ux-alert-tips type="primary" :show-icon="true" title="const { info, deviceBrand, platform } = useDeviceInfo()" />
<ux-gap :height="8" />
<ux-alert-tips type="primary" :show-icon="true" title="设备信息运行期不变,模块级只采集一次" />
<ux-gap :height="8" />
<ux-alert-tips type="primary" :show-icon="true" title="H5 端部分字段为空串,展示层用 $u.strOr 回退" />
<ux-gap :height="8" />
<ux-alert-tips type="primary" :show-icon="true" title="屏幕方向变化请结合 useWindowInfo 的窗口宽高判断" />
</template>
<script setup lang="ts">
import { useDeviceInfo } from '@/uni_modules/uview-pro-x';
const { info, deviceBrand, deviceModel, platform, osName } = useDeviceInfo();
</script>API
Parameters
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| - | This hook takes no arguments | - | - | - |
Returns
| Field | Description | Type |
|---|---|---|
| info | The complete device info object | Ref<DeviceInfo> |
| deviceBrand | Device brand (e.g. HUAWEI, Apple), may be an empty string on some platforms | ComputedRef<string> |
| deviceModel | Device model, may be an empty string on some platforms | ComputedRef<string> |
| deviceType | Device type: phone / pad / pc, etc. | ComputedRef<string> |
| deviceOrientation | Orientation: portrait / landscape, a snapshot at collection time | ComputedRef<string> |
| platform | Runtime platform: ios / android / harmonyos / macos / windows / linux / web, etc. | ComputedRef<string> |
| osName | Operating system name | ComputedRef<string> |
| osVersion | Operating system version | ComputedRef<string> |
| system | Combined OS name and version identifier | ComputedRef<string> |
Notes
- Device info is collected once at module level, and every caller shares the same
info; it is not refreshed at runtime. - The source falls back from
nullfields returned byuni.getDeviceInfo()to'', so some fields may be empty strings on some platforms; check for emptiness or fall back before displaying. deviceOrientationis a snapshot taken when the page is created and does not update on rotation; for orientation changes use the window width and height fromuseWindowInfo.- All fields come from
uni.getDeviceInfo(); the source comment notes thatgetSystemInfoSynchas been split up and this API is the authority for device-level info.