Skip to content

useDeviceInfo 设备信息 ​

17:43

说明 ​

useDeviceInfo 采集 uni.getDeviceInfo() 的品牌、型号、平台与系统等字段,运行期只读;设备信息在模块级只采集一次,所有调用方共享同一份数据。

示例 ​

设备身份 ​

读取 deviceBrand、deviceModel 与 deviceType,字段为空串时用 $u.strOr 回退为 -。

查看源码
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>

系统与平台 ​

读取 platform、osName、osVersion 与 system,同样在展示层对空串做回退。

查看源码
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>

屏幕方向 ​

读取 deviceOrientation 的快照值,页面创建后横竖屏切换不会自动更新。

查看源码
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>

使用说明 ​

以提示条说明设备信息运行期不变、H5 端部分字段为空串、方向变化需结合 useWindowInfo 的窗口宽高判断。

查看源码
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 ​

参数 ​

参数说明类型默认值版本
-该 Hook 不接受参数---

返回值 ​

字段说明类型
info完整设备信息对象Ref<DeviceInfo>
deviceBrand设备品牌(如 HUAWEI、Apple),部分平台可能为空字符串ComputedRef<string>
deviceModel设备型号,部分平台可能为空字符串ComputedRef<string>
deviceType设备类型:phone / pad / pc 等ComputedRef<string>
deviceOrientation横竖屏方向:portrait / landscape,采集时的快照ComputedRef<string>
platform运行平台标识:ios / android / harmonyos / macos / windows / linux / web 等ComputedRef<string>
osName操作系统名称ComputedRef<string>
osVersion操作系统版本ComputedRef<string>
system操作系统名称与版本组合标识ComputedRef<string>

注意事项 ​

  • 设备信息在模块级只采集一次,所有调用共享同一份 info,运行期不再刷新。
  • 源码把 uni.getDeviceInfo() 返回的 null 字段回落为 '',部分平台字段可能为空字符串,展示前建议判空或回退。
  • deviceOrientation 是页面创建时的快照,横竖屏切换不会自动更新;方向变化请结合 useWindowInfo 的窗口宽高判断。
  • 字段统一取自 uni.getDeviceInfo();源码注释说明 getSystemInfoSync 已拆分,设备维度信息以该接口为准。