uni-app项目根据后端返回数据改变元素背景色,使用ref获取不到dom
if(data.msg.errcode === 0){
that.code = data.msg.position
}else if(data.msg.errcode === 40000){
that.code = data.msg.errmsg
that.$refs.r1.$el.style='background:blue'
<view class="container" :class="{errorClass: code === 40000}"></view>
.errorClass{background:blue}
if(data.msg.errcode === 0){
that.code = data.msg.position
}else if(data.msg.errcode === 40000){
that.code = data.msg.errmsg
为什么要这么写?直接绑定class
1.小程序直接操作元素并不支持
2.可以通过:class 和 :style来动态绑定class和相关样式