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

VueJS: v-bind:style only working conditionally in v-for

绑定时使用 v-bind:style 工作正常 color:

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

但是,绑定到 background-color 不起作用:

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

绑定到 border-top:

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

是什么导致它如此有条件地工作?

<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>

如果您使用不是有效标识符的键名,则必须正确引用对象键。所以 v-bind:style="{ background-color: '#' + tradingCardOption.BorderColorHex}"

必须

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

因为 background-color 不能用作对象 属性 键,除非用引号括起来。与 border-color 相同,它应该是:

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

基本上,您需要确保解析器不会尝试将 - 字符解释为负号,然后认为 border 是一个变量。