q***@163.com
q***@163.com
  • 发布:2022-07-12 10:56
  • 更新:2022-07-12 13:26
  • 阅读:349

uni-app android refs 没有style

分类:uni-app
                let el = this.$refs[this.refName];  
                if (el) {  
                    el.style.color = this.attr.background;  
                    el.style.background = this.attr.color;  
                }

上述代码,在H5中正常,在android下提示
TypeError: Cannot set property 'color' of undefined

2022-07-12 10:56 负责人:无 分享
已邀请:
DCloud_UNI_WZF

DCloud_UNI_WZF

因为在 APP 端视图层在 webview 中,而逻辑层在JSCore 中,所以逻辑层无法直接操作 dom
可以通过数据驱动来修改dom样式,类似如下:

<template>  
    <view>  
        <view class="test-dom" :style="{color,background}">test-dom</view>  
    </view>  
</template>  
<script>  
    export default {  
        data() {  
            return {  
                color:'#fff',  
                background:'green'  
            }  
        },  
        onReady(){  
            this.color = 'red'  
            this.background = 'yellow'  
        }  
    }  
</script>  
<style>  
    .test-dom{  
        width: 100px;  
        height: 100px;  
        font-size: 14px;  
    }  
</style>

要回复问题请先登录注册