在$ refs中添加样式?

BGT*_*ate 6 javascript vue.js

无法在$ refs属性中添加样式.无法设置未定义的属性'cssText'.这可能吗?找不到任何类似的东西

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

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

VueComponent {_uid: 493, _isVue: true, $options:
Run Code Online (Sandbox Code Playgroud)

Ben*_*idt 7

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

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

$ref.style.backgroundColor = '#66bb6a';
$ref.style.border = '1px solid #66bb6a';
...
Run Code Online (Sandbox Code Playgroud)

更好的方法是直接在模板中使用Vue的功能:

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