我正在尝试向表中添加一些计算列(请参阅最后三列)。我有一种感觉,这与计算属性未正确引用记录有关。我肯定缺少一些简单的东西!有任何想法吗?谢谢!
这是 fiddle :https://jsfiddle.net/0770ct39/2/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Vue.js Tutorial | More Computed Properties</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div id="app" class="container">
<div class="row">
<table class="table table-hover">
<thead>
<tr>
<th>Phase</th>
<th>Labour Budget</th>
<th>Labour Hours</th>
<th>Labour Cost Rate</th>
<th>Labour Cost</th>
<th>Overhead Cost</th>
<th>Net</th>
</tr>
</thead>
<tbody>
<tr v-for="record in records">
<td>{{record.phase}}</td>
<td>{{record.labourBudget}}</td>
<td><input type="text" v-model="record.labourHours"></td>
<td><input type="text" v-model="record.labourCostRate"></td>
<td>{{record.labourCost}}</td>
<td>{{record.overheadCost}}</td>
<td>{{record.net}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
<script src="https://unpkg.com/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4b2b1a184f6eaf4eaf7" rel="noreferrer noopener nofollow">[email protected]</a>/dist/vue.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
records: [
{phase:"Phase1", labourBudget: 100, labourHours: 4, labourCostRate: 45},
{phase:"Phase2", labourBudget: 100, labourHours: 2, labourCostRate: 42}
]
},
computed: {
labourCost: function(){
return this.record.labourHours * this.record.labourCostRate;
},
overheadCost: function(){
return this.record.labourCost * 1.36;
},
net: function(){
return this.record.netRevenue-this.record.labourCost-this.record.overheadCost;
}
}
})
</script>
</html>
最佳答案
您的计算属性函数不起作用的原因是 this
关键字引用您的 Vue 实例。例如..如果您像这样更改了计算函数...
computed: {
labourCost: function() {
return app.record.labourHours * app.record.labourCostRate;
}
}
...它在功能上与您现在拥有的相同,因为 app
变量引用您的 Vue 实例。
所以,在你当前的状态下,当 Vue 处理你的计算属性时,它会说“嘿!没有名为 record 的数据属性!我的意思是,我看到一个名为 records
的数据属性,但没有一个名为 记录
”。
我建议像这样返回所有记录的计算数组。
var app = new Vue({
el: '#app',
data: {
records: [
{phase:"Phase1", labourBudget: 100, labourHours: 4, labourCostRate: 45},
{phase:"Phase2", labourBudget: 100, labourHours: 2, labourCostRate: 42}
]
},
computed: {
rows: function() {
return this.records.map(function(record) {
return Object.assign({}, record, {
labourCost : record.labourHours * record.labourCostRate,
overheadCost : record.labourCost * 1.36,
net : record.netRevenue-record.labourCost-record.overheadCost
});
});
}
}
})
然后将 html 中的循环更改为
<tr v-for="record in rows">
注:Object.assign()是ES2015的事情。但你可以使用这个polyfill 。或者您可以使用替代对象合并函数,例如 lodash 的 _.assign或jQuery.extend() .
关于javascript - Vue.js 2 - v-for 列表转发器中的计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41046927/