

uni-app has encapsulated commonly used components and JS API into the framework. Developers can ensure multi-platform compatibility by developing according to uni-app specification, and most businesses can be directly satisfied.
Each platform has its own characteristics, so there will be some situations that can not be cross-platform.
In C language, different codes are compiled for windows , mac and other os, by means of #ifdef and #ifndef.
uni-app
refers to this idea, provides a conditional compilation method for uni-app
, and elegantly completes the platform personalized realization in a project.
Conditional compilation is marked with special comments which are the basic of compiling the code inside these comments to different platforms during compilation.
**Writing: **Start with #ifdef or #ifndef plus %PLATFORM% and end with #endif.
Writing method of conditional compilation | Instruction |
---|---|
#ifdef APP Code that requires conditional compilation #endif | Code that only appears on the App platform |
#ifndef H5 Code that requires conditional compilation #endif | Codes that exist on all platforms except the H5 platform |
#ifdef H5 || APP Code that requires conditional compilation #endif | The code that exists on the H5 platform or App platform (There is only || but not &&, because there is no intersection) |
The possible values of %PLATFORM% are as follows:
Value | Effective conditions |
---|---|
VUE3 | HBuilderX 3.2.0+ details (opens new window) |
APP | App |
APP-NVUE | App nvue |
H5 | H5 |
Supported files
Notice:
// Comments
, css uses /* Comments */
, and vue/nvue template uses <!-- Comments -->
;Pre-compilation
and Post-compilation
files, for example, there should be no extra commas in the json file;VUE3
needs to configure "vueVersion" : "3"
at the root node of the project's manifest.json
file// #ifdef %PLATFORM%
...
// #endif
For example, the following codes only appear on App:
For example, the following codes will not appear on the H5 platform:
In addition to conditional compilation on a single platform, it also supports simultaneous compilation on multiple platforms. Use || to separate the platform names.
For example, the following codes will appear on App and H5 platforms:
<!-- #ifdef %PLATFORM% -->
...
<!-- #endif -->
For example, the following components will only appear on App:
<view>
<view>
<!-- #ifdef APP -->
<my-test></my-test>
<!-- #endif -->
</view>
</view>
/* #ifdef %PLATFORM% */
...
/* #endif */
Notice: For style conditional compilation, whether it is css or sass/scss/less/stylus and other pre-compiled languages, you must use the wording of /*Comments*/
.
Correct writing
Wrong writing
The following pages will only be compiled when running to App.
Specific functions of different platforms can be better implemented with conditional compilation of pages.json. In this way, no extra resources will be generated on other platforms so as to reduce the package size.
For conditional compilation of json, if the key names of different platforms are the same, the verifiers installed by developers under cli project will report errors, and the verification rules of these verifiers for the same key of json need to be closed by themselves. If the verifier of HBuilderX is used, there is no need to care about this problem, because the syntax verifier of HBuilderX has been optimized for this purpose.
In different platforms, there may be differences in the referenced static resources. This problem can be solved by conditional compilation of static. Create a dedicated directory for different platforms under the static directory (the directory name is the same as the %PLATFORM%
value range, but the letters are all lowercase)), the static resources in the dedicated directory will only be compiled on a specific platform.
As shown in the following directory structure, a.png
will be compiled only on the App platform, and b.png
will be compiled on all platforms.
┌─static
│ ├─app-plus
│ │ └─a.png
│ └─b.png
├─main.js
├─App.vue
├─manifest.json
└─pages.json
If you want to separate the page files of each platform more thoroughly, you can also create a platforms
directory in the root directory of the uni-app project, and then create further subdirectories such as app-plus
below to store files of different platforms.
Notice
platforms
directory only supports the placement of page files (that is, page vue files). If conditional compilation of other resources is required, it is recommended to use conditional compilation in the static directory (opens new window)HBuilderX provides rich support for conditional compilation of uni-app
:
Code block support
When developing uni-app
in HBuilderX, you can quickly generate code fragments for conditional compilation by entering ifdef
Syntax highlighting
HBuilderX provides syntax highlighting for the code comment part of conditional comments, which can distinguish whether the writing mode is correct or not, making the code clearer (For standalone js files, you need to switch the javascript es6+ editor in the lower right corner of the editor. Standalone css files do not support highlighting, which does not affect the use effect)
Correct comment and quick selection
In HBuilderX, ctrl+alt+/ can generate correct comments (js: // Comments
, css: /* Comments */
, vue/nvue template: <!-- Comments -->
).
Click ifdef or endif to quickly select the conditional compilation part; Click the folding icon on the left to collapse part of the conditional compilation code.
ifios
, ifAndroid
code blocks, which is convenient for writing and judging.