小工具知识网

当前位置: 首页 > 行业知识 > uniapp开发小程序-如何判断小程序是在手机端还是pc端打开

行业知识

uniapp开发小程序-如何判断小程序是在手机端还是pc端打开

2026-11-09 12:36:26 来源:互联网转载

官方说明

https://developers.weixin.qq.com/miniprogram/dev/devtools/pc-dev.html

小程序如何判断是 PC 平台?

通过uni.getSystemInfoSync().platform api获取平台值,如果值是windows或者mac说明是pc端

App.vue

export default {  onLaunch: function () {    uni.setStorageSync("platform", uni.getSystemInfoSync().platform);    uni.setStorageSync("devices", uni.getSystemInfoSync());    uni.setStorageSync(      "windowWidth",      uni.getSystemInfoSync().windowWidth     );    uni.setStorageSync(      "windowHeight",      uni.getSystemInfoSync().windowHeight     );  },};

判断是否是pc端的函数(如果宽度足够也视为pc端,根据实际需求自行调整)

/** 是否是pc端 */function isPC() {  if (["windows", "mac"].some((v) => v === uni.getStorageSync("platform"))) {    return true;  }  if (Number(uni.getStorageSync("windowWidth")) > 1000) {    return true;  }  return false;}

需要注意微信开发者工具的platform值是devtools,只能体验版中测试真实的platform值

上一篇:揭秘在线表格源码解析,工作原理与应用探究

下一篇:python服务器挂了怎么办?