【问题标题】:How to pass a computed property as a style property value for a component in Vue.js?如何将计算属性作为 Vue.js 中组件的样式属性值传递?
【发布时间】:2018-06-15 15:33:46
【问题描述】:

我的组件的模板包含这个 html 元素:

.grid-item(:style="{ width: columnWidth, backgroundColor: 'blue' }")

我想使用计算属性设置它的宽度值:

computed: {
  columnWidth () {
    return ((this.maxWidth - ( this.marginWidth * 2)) - ((this.columnsCount - 1) * this.gutterWidth)) / this.columnsCount;
  }
}

我怎样才能以正确的方式实现这一目标?

问题显然出在:style="{ width: columnWidth 部分 因为当我设置这个 width:'20px' 时,每件事都会完美运行。

【问题讨论】:

  • 您可能需要附加一些单位。
  • 很有可能!我正在查找执行此操作的正确语法。
  • 谢谢!它在官方 Vue 文档中。

标签: javascript css vue.js


【解决方案1】:

我必须在内联样式中将一个单位附加到 columnWidth 值。

width: columnWidth + 'px' 成功了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 2017-12-25
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多