前言:
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