uniapp的条件编译

前言:

  uniapp支持多平台,但遇到平台差异的时候怎么办?这就需要条件编译

正文:

  1.组件(view类)的条件编译 

    <!--  #ifdef  MP-WEIXIN -->
      <button>此代码仅在微信小程序平台出现</button>
    <!--  #endif -->
    <!--  #ifndef H5 -->
        <button>此代码不会在H5平台出现</button>
    <!--  #endif -->
    <!--  #ifdef APP-PLUS || H5 -->
        <button>此代码会在App和H5平台出现</button>
    <!--  #endif -->

  2.样式(css类)的条件编译

/*  #ifdef  APP-PLUS  */
    /* 仅在app平台显示 */
    .abc{
        color:#fff;
    }
/*  #endif  */

  3.api(js类)的条件编译

        //  #ifdef  APP-PLUS
            console.log('此代码仅在App平台显示');
        //  #endif
        //  #ifndef H5
            console.log("此代码不会在H5平台显示");
        //  #endif
        //  #ifdef APP-PLUS || H5
            console.log("此代码会在App平台和H5平台显示");
        //  #endif

  4.pages.json 的条件编译

//  #ifdef  APP-PLUS
    {
        "path":"pages/index/index"
    }
//  #endif

猜你喜欢

转载自www.cnblogs.com/xihailong/p/13397853.html