上传预览图片 条件注释实现跨段兼容

上传图片

  • uni.chooseImage方法从本地相册选择图片或使用相机拍照。
  • 图片参考
<template>
	<view>
		<view>hello-uni</view>
		
		<button type="primary" @click="chooseImg">上传图片</button>
		<image v-for="item in imgArr":src="item" @click="previewImg(item)"></image>
	</view>
	
</template>
<script>
	export default{
		data(){
			return{
				imgArr:[]
			}
		},
		methods:{
			chooseImg(){
				uni.chooseImage({
					count:5,
					success:res=> {
						this.imgArr = res.tempFilePaths
					}
				})
			},
	}
</script>

预览图片

previewImg(current){
	console.log(current)
	uni.previewImage({
		current,
		urls:this.imgArr,
		loop:true, //app中有效
		indicator:'Number'
	})
}

条件注释实现跨段兼容

  • 条件编译是用特殊的注释作为标记,在编译时根据这些特殊的注释,将注释里面的代码编译到不同平台。
  • **写法:**以 #ifdef 加平台标识 开头,以 #endif 结尾。

选中要注释的代码段,ctr+alt+/

组件的条件注释

<template>
	<view>
		<!-- #ifdef H5 -->
		<view>我只希望在H5中出现</view>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<view>我只希望在小程序中出现</view>
		<!-- #endif -->
		
	</view>
	
</template>

api的条件注释

<script>
	export default{
		methods:{
			onLoad(){
				// #ifdef H5
				console.log("我希望在H5中打印")
				// #endif
				// #ifdef MP-WEIXIN
				console.log("我希望在小程序中打印")
				// #endif
			}
		}
	}
</script>

样式的条件注释

<style>
	/* #ifdef H5 */
	view{
		color: hotpink;
	}
	/* #endif */
	/* #ifdef MP-WEIXIN */
	view{
		color: #007AFF;
	}
	/* #endif */
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值