# 如何判断当前环境是移动端还是PC端

Issue

欢迎在 Issue 中交流与讨论: Issue 211 (opens new window)

判断 navigator.userAgent,对于 Android/iPhone 可以匹配以下正则

const appleIphone = /iPhone/i;
const appleIpod = /iPod/i;
const appleTablet = /iPad/i;
const androidPhone = /\bAndroid(?:.+)Mobile\b/i; // Match 'Android' AND 'Mobile'
const androidTablet = /Android/i;

当然,不要重复造轮子,推荐一个库: https://github.com/kaimallea/isMobile (opens new window)

import isMobile from 'ismobilejs'

const mobile = isMobile()
Last Updated: 1/17/2021, 2:02:56 PM