Computed 属性 - price netto 和每件商品的价格
Computed property - price netto and price per one item
我有一系列带有数量和价格(税前)的商品。我想计算每件商品的净价和价格。我还有一个允许显示货币的过滤器,并希望在创建的计算 属性 旁边显示该过滤器。计算属性和在它们旁边显示过滤器不起作用。我该怎么做才能让它们发挥作用?
JSFiddle here
HTML:
<table>
<thead>
<tr class="table-head">
<th v-for="item in tableHead">{{ item.name }} <span>{{ item.desc }}</span></th>
</tr>
</thead>
<tbody>
<tr v-for="(element, index) in amount">
<td>{{ element.amount }}</td>
<td>
{{ priceNet }}
</td>
<td>
{{ element.price | curr }}
{{ pricePerItem }}
</td>
</tr>
</tbody>
</table>
Vue JS:
new Vue({
el: "#app",
data: {
tableHead: [
{ name: 'amount', desc: '' },
{ name: 'price', desc: '(net)' },
{ name: 'price', desc: '(pre-tax)' }
],
amount: [
{ amount: 100, price: 80.61 },
{ amount: 250, price: 72.10 },
{ amount: 500, price: 79.62 },
{ amount: 1000, price: 100.20 },
{ amount: 2500, price: 147.60 },
{ amount: 5000, price: 232.56 }
]
},
computed: {
priceNet() {
this.amount.forEach((element) => {
let net = (element.price / 1.23);
return net;
})
},
pricePerItem() {
this.amount.forEach((element) => {
let priceP = element.price / element.amount;
return priceP;
})
}
},
filters: {
curr: (value) => {
return `${value.toFixed(2)} euro`
}
}
})
而不是 computed
你想要 methods
:
methods: {
priceNet(price) {
return price / 1.23
},
pricePerItem(price, amount) {
return price / amount
}
},
然后,在您的 html 中更新绑定:
<tr v-for="(element, index) in amount">
<td>{{ element.amount }}</td>
<td>{{ priceNet(element.price) | curr }}</td>
<td>
{{ element.price | curr }}
{{ pricePerItem(element.price, element.amount) | curr }}
</td>
</tr>
已更新fiddle:
我有一系列带有数量和价格(税前)的商品。我想计算每件商品的净价和价格。我还有一个允许显示货币的过滤器,并希望在创建的计算 属性 旁边显示该过滤器。计算属性和在它们旁边显示过滤器不起作用。我该怎么做才能让它们发挥作用? JSFiddle here
HTML:
<table>
<thead>
<tr class="table-head">
<th v-for="item in tableHead">{{ item.name }} <span>{{ item.desc }}</span></th>
</tr>
</thead>
<tbody>
<tr v-for="(element, index) in amount">
<td>{{ element.amount }}</td>
<td>
{{ priceNet }}
</td>
<td>
{{ element.price | curr }}
{{ pricePerItem }}
</td>
</tr>
</tbody>
</table>
Vue JS:
new Vue({
el: "#app",
data: {
tableHead: [
{ name: 'amount', desc: '' },
{ name: 'price', desc: '(net)' },
{ name: 'price', desc: '(pre-tax)' }
],
amount: [
{ amount: 100, price: 80.61 },
{ amount: 250, price: 72.10 },
{ amount: 500, price: 79.62 },
{ amount: 1000, price: 100.20 },
{ amount: 2500, price: 147.60 },
{ amount: 5000, price: 232.56 }
]
},
computed: {
priceNet() {
this.amount.forEach((element) => {
let net = (element.price / 1.23);
return net;
})
},
pricePerItem() {
this.amount.forEach((element) => {
let priceP = element.price / element.amount;
return priceP;
})
}
},
filters: {
curr: (value) => {
return `${value.toFixed(2)} euro`
}
}
})
而不是 computed
你想要 methods
:
methods: {
priceNet(price) {
return price / 1.23
},
pricePerItem(price, amount) {
return price / amount
}
},
然后,在您的 html 中更新绑定:
<tr v-for="(element, index) in amount">
<td>{{ element.amount }}</td>
<td>{{ priceNet(element.price) | curr }}</td>
<td>
{{ element.price | curr }}
{{ pricePerItem(element.price, element.amount) | curr }}
</td>
</tr>
已更新fiddle: