【问题标题】:How to display rows in Vue.js with different colors [duplicate]如何在 Vue.js 中显示不同颜色的行 [重复]
【发布时间】:2020-07-24 16:22:14
【问题描述】:

我不是前端开发人员,所以 Vue 和 JS 对我来说是新的。我正在编写管理销售的应用程序。

在其中一个组件中,我想将发票显示为列表。为了更好地查看这些行,我提出了将一行设置为白色、下一个灰色、下一个白色、下一个灰色等等的想法。

这是我的问题:如何在 Vue 中做到这一点?

我尝试了类似的方法,但它不起作用(我只是删除了 li 项目,因为这些对那个例子没有用):

<li v-for="fruit in fruits" v-bind:key="fruit.id" :style="{backgroundColor: color}" v-bind="counter++"> 
</li>

这是我的 Vue 实例:

export default {
    data() {
        return {
            color: '',
            counter: 0,
            fruits: ["Lorem isum", "Lorem isum", "Lorem isum", "Lorem isum"]

        }
    }, methods: {
        choseColor() {
            if (this.counter % 2 !== 0 ) {
                color = 'grey'
            } else {
                color = 'white'
            }
        }
    }
} 

【问题讨论】:

标签: javascript html css vue.js


【解决方案1】:

改变

:style="{backgroundColor: color}"

:style="'background: ' + choseColor()'"

choseColor() {
            return ((this.counter % 2) !== 0) ? `grey` : 'white';
}

试试看。它应该解决它。

【讨论】:

    猜你喜欢
    • 2014-03-13
    • 1970-01-01
    • 2020-09-26
    • 2014-09-11
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2014-12-22
    相关资源
    最近更新 更多