简体   繁体   English

VueJS:v-bind:样式仅在v-for中有条件地工作

[英]VueJS: v-bind:style only working conditionally in v-for

Using v-bind:style works fine when binding color : 使用v-bind:style在绑定color时工作正常:

<p v-bind:style="{ color: '#' + tradingCardOption.BorderColorHex }">
  {{ tradingCardOption.ColorSetName }}
</p>

But, binding to the background-color does not work: 但是,绑定到background-color不起作用:

v-bind:style="{ background-color: '#' + tradingCardOption.BorderColorHex }" 

And neither does binding to border-top : 也没有绑定到border-top

v-bind:style="{ border-top: 15px solid + '#' + tradingCardOption.CornerColorHex }"

What could cause this to work so conditionally? 是什么导致这种条件有条不紊地工作?

 <div v-for="tradingCardOption in tradingCardOptions"> <div v-bind:style="{ background-color: '#' + tradingCardOption.BorderColorHex}" class="color-swatch " v-bind:class="{'selected' : $index == 0}" v-on:click="selectTradingCardOption(tradingCardOption, $event)"> <div v-bind:style="{ border-top: 15px solid + '#' + tradingCardOption.CornerColorHex}"></div> </div> {{ tradingCardOption.BorderColorHex}} <p v-bind:style="{ color: '#' + tradingCardOption.BorderColorHex}"> {{ tradingCardOption.ColorSetName }}</p> </div> 

Object keys must be properly quoted if you use key names that are not valid identifiers. 如果使用非有效标识符的键名,则必须正确引用对象键。 So v-bind:style="{ background-color: '#' + tradingCardOption.BorderColorHex}" 所以v-bind:style="{ background-color: '#' + tradingCardOption.BorderColorHex}"

must be 一定是

v-bind:style="{'background-color': '#' + tradingCardOption.BorderColorHex}"

because background-color can't be used as object property key unless surrounded with quotes. 因为background-color不能用作对象属性键,除非用引号括起来。 Same with border-color it should be: border-color相同,它应该是:

{'border-top': '15px solid #' + tradingCardOption.CornerColorHex}

Basically, you need to make sure parser doesn't try to interpret - character as minus and then think that border is a variable. 基本上,你需要确保解析器不会尝试解释-字符为减号,然后认为border是一个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM