当前位置:网站首页>Uni app conditional compilation ifdef ENDIF compatible with multiple terminals
Uni app conditional compilation ifdef ENDIF compatible with multiple terminals
2022-07-19 05:10:00 【A young man】
On a project , When requesting data interface eval Function in H5 and APP The end can be rendered , But the page reports an error when running the applet . Then change JSON.parse() Method , I also found that small programs can be rendered , but H5 and APP The end is wrong again . The result is a search on the Internet , Finally, a solution was found , Cross end compatibility , It is the conditional compilation that we will talk about next .
Conditional compilation is actually if Judge , With #ifdef or #ifndef Add %PLATFORM% start , With #endif ending . Personally, this is uniapp Put your own js Encapsulate into the framework , Just follow uni-app Standardized development can ensure multi platform compatibility . Just mark with special comments , Compile according to these special comments , Compile the code in the comments to different platforms .
Conditional compilation is implemented with annotations , There are different ways to write notes in different grammars ,js Use // notes 、 css Use / notes / **、 vue/nvue Use... In the template <!– notes – >
js Use :
// #ifdef APP-PLUS
this.swiperList = JSON.parse(res.data).postss;
// #endif
vue Page usage :
<!-- #ifdef APP-PLUS -->
<view :style="{ height: iStatusBarHeight + 'px'}" class="stat"></view>
<!-- #endif -->
css Use :
/* #ifdef APP-PLUS */
width: 60rpx;
height: 60rpx;
/* #endif */
Sometimes, because of requirements, only some code exists on some two platforms . Then use this || , Out-of-service &&, Because there is no intersection . The code example is shown below :
<!-- #ifdef H5 || MP-WEIXIN -->
<view :style="{ height: iStatusBarHeight + 'px'}" class="stat"></view>
<!-- #endif -->
<!-- #ifdef APP-PLUS || MP-WEIXIN -->
<view :style="{ height: iStatusBarHeight + 'px'}" class="stat"></view>
<!-- #endif -->
<!-- #ifdef H5 || APP-PLUS -->
<view :style="{ height: iStatusBarHeight + 'px'}" class="stat"></view>
<!-- #endif -->
swiper() {
uni.request({
url: 'https://www.fastmock.site/mock/745d/shop/api/swiper',
method: 'GET',
dataType: 'JSON',
data: {
text: 'uni.request'
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: (res) => {
console.log(res.data);
// #ifdef APP-PLUS || H5
this.swiperList = res.data.postss;
// #endif
// #ifdef MP-WEIXIN
this.swiperList = JSON.parse(res.data).postss;
// #endif
}
});
},
The above is how to use conditional compilation , It's just personal understanding , Take a note . If there is any deficiency , A lot of correct .
边栏推荐
猜你喜欢

用户登录-以及创建验短信证码

Harmonyos fourth training notes

HarmonyOS第二次培训笔记

关于New_Online_Judge_1081_哥德巴赫猜想的思考

C语言 带你 手撕 通讯录

基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置

Implementation idea of log adding to database
![[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)](/img/e6/beea0bb0a9f4b41206c6fcb130fdfd.png)
[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)

【C语言_学习_考试_复习第三课】ASCII码与C语言概述

elment-ui使用方法
随机推荐
Desensitization field example
租用服务器,以及部署在pycharm专业版上的pytorch环境训练yolov5模型教程服务器环境安装库文件:
SQL statement learning
数据分析与数据挖掘实战案例本地房价预测(716):
POC——DVWA‘s File Inclusion
轮播图移动速度(匀速,缓动)案例归总
获取URL参数的两种方法及location对象的各项获取方式
HarmonyOS第二次培训笔记
Asynchronous data SMS verification code
学习C语言第7天
POC——DVWA‘s XSS Reflected
读论文《SNUNet-CD: A Densely Connected Siamese Network for Change Detection of VHR Images》
Teddy Cup title a full version optimization update (4/23)
User - registration / login
这么6的刷题网站你不会没听说过吧?你已经out 了?
ModelArts第二次培訓筆記
Fanoutexchange switch is simple to use
User management - paging
User login - and create SMS verification code
Feature extraction of machine learning (digitization and discretization of category features and digitization of text features)