UNI-APP_uni-app中的#ifdef #ifndef #endif的用处,实现兼容多端平台

一、#ifdef #ifndef #endif三者的意思

属性 作用
#ifdef 仅在某个平台上使用
#ifndef 在除了这个平台的其他平台上使用(非此平台使用)
#endif 结束条件编译

二、2.平台标识的意思

标识 平台
APP-PLUS 5+App
MP 微信小程序/支付宝小程序/百度小程序/头条小程序/QQ小程序
MP-WEIXIN 微信小程序
MP-ALIPAY 支付宝小程序
MP-BAIDU 百度小程序
MP-TOUTIAO 头条小程序
MP-QQ QQ小程序
H5 H5
APP-PLUS-NVUE 5+App nvue

在Hbuilder编辑器中输入#号会有相应提示
UNI-APP_uni-app中的#ifdef #ifndef #endif的用处,实现兼容多端平台_第1张图片

三、结合使用例子

例子1(仅在5+app平台上此代码生效)

/*
	js使用// html使用 
*/
<!-- #ifdef APP-PLUS -->
<view>仅在5+app平台上显示代码</view>
<!-- #endif -->

methods: {
	// #ifdef APP-PLUS
	click(){
		console.log('仅在5+app平台上生效的事件')
	},
	// #endif
}

例子2(非5+app平台上此代码生效)

/*
	js使用// html使用 
*/
<!-- #ifndef APP-PLUS -->
 <view>只要不是5+app平台上此代码会显示</view>
<!-- #endif -->

methods: {
	// #ifndef APP-PLUS
	click(){
		console.log('非5+app平台上生效的事件')
	},
	// #endif
}

css兼容

/* #ifdef H5 */
	height: calc(100vh - 44px);
/* #endif */

你可能感兴趣的:(#,UNI-APP,css,前端)