在 $refs 中添加样式?

新手上路,请多包涵

无法在 $refs 属性中添加样式。无法设置未定义的属性“cssText”。这可能吗?找不到类似的

this.$refs['ticketCode_'+this.resoTrans_id].style.cssText =
                "background-color:#66BB6A !important; border:1px solid #66BB6A !important; color:#fff !important;";

打印没有 .style 似乎工作正常 console.log(this.$refs['ticketCode_'+this.resoTrans_id])

 VueComponent {_uid: 493, _isVue: true, $options:

原文由 BGTabulation BGTabulate 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 602
1 个回答

您不应该使用 cssText 而是使用 JavaScript API 来设置样式(您也可以在 $refs 上执行此操作):

 let $ref = this.$refs['ticketCode_' + this.resoTrans_id]

$ref.style.backgroundColor = '#66bb6a';
$ref.style.border = '1px solid #66bb6a';
...

更好的是直接在模板中利用 Vue 的强大功能:

 <your-element ref="'ticketCode_' + resoTrans_id" :style="{ backgroundColor: '#66bb6a', border: '1px solid #66bb6a' /* ... */ }" />

原文由 Benjamin Schmidt 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏