uni-app的条件编译跨端兼容

老子叫甜甜 提交于 2020-09-27 17:08:48

一 点睛

1 官网

http://uniapp.dcloud.io/platform?id=%E8%B7%A8%E7%AB%AF%E5%85%BC%E5%AE%B9

二 代码

1 修改 phone.vue

<template>
	<view>
		<button type="primary" @click="updatePhoto">上传图片</button>
		<image v-for="item in imgArr" :src="item" @click="previewImg(item)"></image>
		<!-- #ifdef H5 -->
		<view>在H5中可见</view>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<view>在微信小程序中可见</view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgArr: []
			}
		},
		methods: {
			updatePhoto() {
				uni.chooseImage({
					count: 5, //默认9
					success: res => {
						this.imgArr = res.tempFilePaths
					}
				});
			},
			previewImg(current) {
				uni.previewImage({
					current: current,
					urls: this.imgArr,
					loop: true,
					indicator: 'number'
				})
			}

		},
		onLoad() {
			// #ifdef H5
			console.log('在h5中打印')
			// #endif
			// #ifdef MP-WEIXIN
			console.log('在微信小程序中打印')
			// #endif
		}
	}
</script>

<style>
	/* h5中的样式 */
	/* #ifdef H5 */
	view {
		color: #0000FF;
	}

	/* #endif */
	/* 微信小程序样式 */
	/* #ifdef MP-WEIXIN */
	view {
		color: #008000;
	}

	/* #endif */
</style>

三 效果

1 H5条件编译效果

2 微信小程序编译效果

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!