允许父页面修改子组件样式,根据环境判断图片引用地址

当想让父页面来决定子组件样式时,需要关闭样式隔离
以uniapp为例,通过options关闭样式隔离,父页面直接传class类名进来就可以使用样式,这兼容app和小程序

/**
 * 注意:其他组件里使用该组件,不能直接传class类名,需要通过style来设置样式
 * 注意:页面使用该组件不能在页面的style上加scoped
 * 注意:在nvue页面不能直接传class类名
 * */
<template>
	<view>
		<!-- 不是app环境使用线上图片 -->
		<!-- #ifndef APP-PLUS -->
		<image :src="static_url + '/applet' + src" :style="cstyle" :class="cclass" @tap="clickImg" :mode="mode" :lazy-load="lazyLoad" :show-menu-by-longpress="showMenuByLongpress" @error="error" @load="load"></image>
		<!-- #endif -->
		
		<!--app环境使用本地图片-->
		<!-- #ifdef APP-PLUS -->
		<image :src="src" :style="cstyle" :class="cclass" @tap="clickImg" :mode="mode" @error="error" @load="load"></image>
		<!-- #endif -->
	</view>
</template>

<script>
	export default{
		name:'image-switch',
		data(){
			return{
				static_url: getApp().globalData.static_url,//静态资源域名
			}
		},
		// #ifdef MP-WEIXIN || MP-TOUTIAO || MP-WEIXIN || MP-ALIPAY || MP-QQ
		//关闭样式隔离
		options: {
			styleIsolation: 'apply-shared'
		},
		// #endif
		props:{
			//图片地址
			src:{
				type:String,
				default:''
			},
			//图片模式
			mode:{
				type:String,
				default:''
			},
			//类名
			cclass:{
				type:String,
				default:''
			},
			//懒加载
			lazyLoad:{
				type:String,
				default:''
			},
			//长按图片显示识别小程序码菜单,仅支持微信小程序
			showMenuByLongpress:{
				type:Boolean,
				default:false
			},
			//样式,在自定义组件里使用该组件,只能通过style来设置样式
			cstyle:{
				type:String,
				default:''
			}
		},
		methods:{
			//图片报错
			error(e){
				this.$emit('error',e)
			},
			//加载完成
			load(e){
				this.$emit('load',e)
			},
			//点击图片
			clickImg(){
				this.$emit('clickImg')
			},
		}
	}
</script>

<style lang="scss">
	
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值