Vue.js 2 - 在 v-for 列表转发器中计算 属性
Vue.js 2 - Computed property in v-for list repeater
我正在尝试将一些计算列添加到 table(请参阅最后三列)。我觉得这与计算的 属性 没有正确引用记录有关。我确定缺少一些简单的东西!有任何想法吗?谢谢!
这是 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/vue@2.0.3/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>
您需要让每一行都有自己的组件并将 record
传递给它,这样才能正常工作。如果您不想制作组件,可以使用 methods
代替。
你基本上可以这样做:
methods: {
laborCost: function(record) {
return record.labourHours * record.labourCostRate
},
...
}
然后像这样使用它
{{ laborCost(record) }}
如果你想走组件路线(我认为你应该这样做)你会做这样的事情:
<record v-for="record in records" :record="record"></record>
然后您可以将这些计算属性复制并粘贴到该组件中,它将按预期工作。
您计算的 属性 函数不起作用的原因是 this
关键字引用了您的 Vue 实例。例如..如果你像这样改变你的计算函数......
computed: {
labourCost: function() {
return app.record.labourHours * app.record.labourCostRate;
}
}
...它在功能上等同于您现在拥有的,因为 app
变量指的是您的 Vue 实例。
因此,在您当前的状态下,当 Vue 处理您的计算属性时,它会显示 "hey! there's no data property named record! I mean, I see one called records
, but none called record
"。
我建议像这样返回所有记录的计算数组。
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() is an ES2015 thing. But you can use this polyfill. Or you can use an alternative object-merge function like lodash's _.assign or jQuery.extend().
我正在尝试将一些计算列添加到 table(请参阅最后三列)。我觉得这与计算的 属性 没有正确引用记录有关。我确定缺少一些简单的东西!有任何想法吗?谢谢!
这是 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/vue@2.0.3/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>
您需要让每一行都有自己的组件并将 record
传递给它,这样才能正常工作。如果您不想制作组件,可以使用 methods
代替。
你基本上可以这样做:
methods: {
laborCost: function(record) {
return record.labourHours * record.labourCostRate
},
...
}
然后像这样使用它
{{ laborCost(record) }}
如果你想走组件路线(我认为你应该这样做)你会做这样的事情:
<record v-for="record in records" :record="record"></record>
然后您可以将这些计算属性复制并粘贴到该组件中,它将按预期工作。
您计算的 属性 函数不起作用的原因是 this
关键字引用了您的 Vue 实例。例如..如果你像这样改变你的计算函数......
computed: {
labourCost: function() {
return app.record.labourHours * app.record.labourCostRate;
}
}
...它在功能上等同于您现在拥有的,因为 app
变量指的是您的 Vue 实例。
因此,在您当前的状态下,当 Vue 处理您的计算属性时,它会显示 "hey! there's no data property named record! I mean, I see one called records
, but none called record
"。
我建议像这样返回所有记录的计算数组。
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() is an ES2015 thing. But you can use this polyfill. Or you can use an alternative object-merge function like lodash's _.assign or jQuery.extend().