Vue.js:class 的计算值

Vue.js: Computed value for class

在我看来我有以下条件 class:

<td class="text-center" v-bind:class="{ positivity }"></td>

在我的组件中我有以下内容:

positivity: function() {
    var type = typeof this.transaction.weeks != "undefined"
    var positive = 'green-bold'

    if ( type ) {
      positive = 'red-bold'
    }

    return positive
}

...但是我计算的 class 显示为:

<td class="text-center positivity"></td>

不管 positivity() 的结果如何。我做错了什么?

你可以做到:

v-bind:class="positivity"

或:

v-bind:class="{ 'green-bold': !positivity, 'red-bold': positivity }"

positivity: function() {
    return typeof this.transaction.weeks != "undefined";
}