运行环境
uni-app 可通过 process.env.NODE_ENV 判断当前环境是开发环境(运行) 还是生产环境(发行) 一般用于连接测试服务器或者生产服务器的动态切换
本地 http:// localhhost:3000
线上 http://yangyay.clud测试服务器: development
生产服务器: production
let baseUrl = null;
if(process.env.NODE_ENV =="development"){
// 本地服务器
baseUrl = ' http:// localhhost:3000'
}else {
// 线上服务器
baseUrl = 'http://yangyay.clud'
}
平台判断
快捷键 ctrl + alt + /
- 如果对一个盒子在不同的平台展示不同样式 可以用条件编译 解决 样式层叠
/* #ifdef H5 */
.box {
width: 100upx;
height: 100upx;
}
/* #endif */
/* #ifdef MP-WEIXIN */
.box {
width: 200upx;
height: 300upx;
}
/* #endif */
- html
<!-- #ifdef H5 -->
<button type="default">在h5端显示</button>
<!-- #endif -->
<!-- #ifdef MP-WEIXIN -->
<button type="default">在微信端显示</button>
<!-- #endif -->
- js
// #ifdef H5
uni.showToast({
title:'h5显示'
})
// #endif
// #ifdef MP-WEIXIN
uni.showToast({
title:'在微信端显示'
})
// #endif
判断机型
if (uni.getSystemInfoSync().platform == 'android') {
console.log("运行在安卓手机上");
} else if (uni.getSystemInfoSync().platform == 'ios') {
console.log("运行在苹果手机上");
} else {
console.log('运行在开发工具上');
}