uni-app条件注释实现跨端兼容

uni-app条件注释实现跨端兼容

概念:条件编译是用特殊的注释作为标记,在编译时根据这些特殊的注释,将注释里面的代码编译到不同平台
写法:以 #ifdef 加平台标识 开头,以 #endif 结尾
  1. 组件的条件注释

<view>
  h5页面会显示
view>


<view>
  微信小程序会显示
view>


<view>
  app会显示
view>

  1. api的条件注释
onLoad () {
  //#ifdef MP-WEIXIN
  console.log('微信小程序')
  //#endif
  //#ifdef H5
  console.log('h5页面')
  //#endif
}
  1. 样式的条件注释
/* #ifdef H5 */
view{
  height: 100px;
  line-height: 100px;
  background: red;
}
/* #endif */
/* #ifdef MP-WEIXIN */
view{
  height: 100px;
  line-height: 100px;
  background: green;
}
/* #endif */

你可能感兴趣的:(uni-app,小程序,uni-app)