Uniapp 条件编译详解

Uniapp 条件编译详解

条件编译是 Uniapp 中非常重要的功能,它允许开发者针对不同平台编写特定的代码,从而实现一套代码多端运行的效果。

一、基本语法

1. 以 #ifdef 或 #ifndef 开头

javascript 复制代码
// #ifdef 平台名称
平台特定代码
// #endif

// #ifndef 平台名称
非该平台代码
// #endif

2. 多平台判断

javascript 复制代码
// #ifdef MP-WEIXIN || H5
微信小程序或H5平台代码
// #endif

二、平台标识符

平台 标识符 说明
微信小程序 MP-WEIXIN
支付宝小程序 MP-ALIPAY
百度小程序 MP-BAIDU
字节跳动小程序 MP-TOUTIAO
QQ小程序 MP-QQ
H5 H5 浏览器环境
App APP 包括APP-PLUS和APP-NVUE
iOS APP-PLUS或IOS App的iOS平台
Android APP-PLUS或ANDROID App的Android平台
快应用 QUICKAPP-WEBVIEW 华为快应用

三、使用场景

1. 模板中使用

html 复制代码
<!-- 只在微信小程序中显示 -->
<!-- #ifdef MP-WEIXIN -->
<view>微信小程序特有内容</view>
<!-- #endif -->

<!-- 在H5和App中显示 -->
<!-- #ifdef H5 || APP -->
<view>H5和App显示的内容</view>
<!-- #endif -->

2. 样式中使用

css 复制代码
/* 只在App中生效的样式 */
/* #ifdef APP */
.my-view {
  padding-top: 20px;
}
/* #endif */

/* 非H5平台的样式 */
/* #ifndef H5 */
.my-text {
  font-size: 16px;
}
/* #endif */

3. JavaScript中使用

javascript 复制代码
// 平台特定的API调用
// #ifdef MP-WEIXIN
wx.login({
  success(res) {
    console.log(res.code);
  }
});
// #endif

// #ifdef H5
console.log('这是在浏览器环境中');
// #endif

4. 页面路由处理

javascript 复制代码
// 不同平台跳转方式不同
function navigateTo(url) {
  // #ifdef H5
  window.location.href = url;
  // #endif
  
  // #ifdef MP-WEIXIN
  wx.navigateTo({ url });
  // #endif
  
  // #ifdef APP
  uni.navigateTo({ url });
  // #endif
}

四、高级用法

1. 多条件组合

javascript 复制代码
// #ifdef MP-WEIXIN || MP-ALIPAY
// 微信或支付宝小程序特有逻辑
// #endif

2. 条件编译块嵌套

javascript 复制代码
// #ifdef APP
// #ifdef IOS
console.log('这是iOS平台');
// #endif
// #ifdef ANDROID
console.log('这是Android平台');
// #endif
// #endif

3. 文件整体条件编译

可以在文件顶部添加条件编译,整个文件只在特定平台被引入:

javascript 复制代码
// #ifdef MP-WEIXIN
// 整个文件只在微信小程序中有效
export default {
  data() {
    return {
      weixinSpecificData: true
    }
  }
}
// #endif

五、manifest.json 中的条件编译

json 复制代码
{
  "mp-weixin": {
    /* 微信小程序特有配置 */
  },
  "h5": {
    /* H5特有配置 */
  },
  "app-plus": {
    /* App特有配置 */
  }
}

六、package.json 中的条件编译

json 复制代码
{
  "uni-app": {
    "scripts": {
      "mp-weixin": {
        "title": "微信小程序"
      },
      "h5": {
        "title": "H5"
      }
    }
  }
}

七、注意事项

  1. 注释语法必须正确 :条件编译注释必须使用 // 而不能使用 /* */
  2. 空格要求#ifdef 和平台名称之间必须有空格
  3. 大小写敏感 :平台标识符必须完全匹配(如 MP-WEIXIN 不能写成 mp-weixin
  4. 文件条件编译:文件整体条件编译时,文件内容必须全部包含在条件编译块中
  5. 性能影响:条件编译不会增加最终包的体积,因为编译时会移除其他平台的代码

八、实际应用示例

1. 平台特定组件

html 复制代码
<template>
  <view>
    <!-- 公共部分 -->
    <view>所有平台都显示的内容</view>
    
    <!-- 平台特定部分 -->
    <!-- #ifdef MP-WEIXIN -->
    <weixin-specific-component />
    <!-- #endif -->
    
    <!-- #ifdef H5 -->
    <h5-specific-component />
    <!-- #endif -->
  </view>
</template>

<script>
// 平台特定的组件引入
// #ifdef MP-WEIXIN
import weixinSpecificComponent from './weixin-component.vue';
// #endif

// #ifdef H5
import h5SpecificComponent from './h5-component.vue';
// #endif

export default {
  components: {
    // #ifdef MP-WEIXIN
    weixinSpecificComponent,
    // #endif
    
    // #ifdef H5
    h5SpecificComponent
    // #endif
  }
}
</script>

2. API 兼容处理

javascript 复制代码
function getStorage(key) {
  // #ifdef MP-WEIXIN
  return wx.getStorageSync(key);
  // #endif
  
  // #ifdef H5
  return localStorage.getItem(key);
  // #endif
  
  // #ifdef APP
  return uni.getStorageSync(key);
  // #endif
}

通过合理使用条件编译,可以大大减少为不同平台维护多套代码的工作量,同时又能保证各平台的体验最佳。

相关推荐
CHB16 小时前
uni-app x 蒸汽模式 iOS版 性能测试基准报告 Benchmark
ios·uni-app·swiftui
衍生星球16 小时前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
编程猪猪侠2 天前
UniAppx安卓app实现左侧导航与右侧内容联动滚动
android·uni-app
千逐682 天前
Uniapp 鸿蒙实战之 AtomGit APP - 仓库详情与文件树浏览
服务器·microsoft·uni-app
万物得其道者成2 天前
uni-app App 热更新实现指南:从接口检查到下载安装重启(附带AI提示词)
uni-app
anyup2 天前
uView Pro 正式支持 MCP 协议!让 AI 秒懂你的组件库
前端·uni-app·ai编程
千逐682 天前
Uniapp 鸿蒙实战之 AtomGit APP - 首页设计与仓库列表实现
华为·uni-app·harmonyos
天丁o3 天前
Spring Boot + uni-app 智慧考勤闭环 Demo:打卡记录、异常状态和日统计如何复用到企业系统
spring boot·uni-app·mybatis plus·企业管理系统·考勤系统
这是个栗子3 天前
uni-app 微信小程序开发:常用事件指令(@xxx)(一)
微信小程序·小程序·uni-app
小徐_23337 天前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app