【发布时间】:2018-07-27 10:04:32
【问题描述】:
我有一个 Vue 文件,它将数据作为 json。我想遍历模板中的问题并选择要添加的 TR。在 Django 模板中,我可以这样做:
{% for foo in foos %}
{% if foo.status == 'status1' %}
<tr class="bg-warning">
{% elif foo.status == 'status2' %}
<tr class="bg-success">
{% elif foo.status == 'status3' %}
<tr class="bg-danger">
{% else %}
<tr class="bg-info">
{% endif %}
我正在尝试像这样在 Vue 中这样做
<tr v-for="foo in foos"
v-bind:class="{
'bg-warning': isReview(foo.status),
'bg-info': isRegistering(foo.status),
'bg-danger': isCancelled(foo.status),
'bg-success': isFinished(foo.status)}">
接下来是我的方法:
computed: {
isReview: function (status) {
if (status === 'status1') {
return true
} else {
return false
}
},
isRegistering: function (status) {
if (status === 'status2') {
return true
} else {
return false
}
},
isCancelled: function (status) {
if (status === 'status3') {
return true
} else {
return false
}
},
isFinished: function (status) {
if (status === 'status4') {
return true
} else {
return false
}
}
}
所以我的问题是如何为每次迭代创建 1 个特定的表行,这取决于 Object.status?
【问题讨论】:
-
你的代码不工作吗?
-
计算属性不能有参数,换个方法试试
-
是的,根本不是创建表行,所以页面是空的
-
谢谢,JFM,问题已解决,将计算更改为方法
标签: vue.js