当想让父页面来决定子组件样式时,需要关闭样式隔离
以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>