javascript - 在 vue.js 中为每个循环使用计算数据
问题描述
v-model
在使用for<input>
标签时,我愿意利用响应式数据更改。
现在我想在循环内有一个值,以便在触发v-for
时自动更新。v-model
我在这里做错了什么?
<tr v-for="(service, key) in services" :key="key">
<td class="left aligned">
<div class="title" contenteditable="true">{{ service.title }}</div>
<div class="desc" contenteditable="true">{{ service.description }}</div>
</td>
<td class="price">
<input v-model.number="service.price" type="number" min="0" /> € / day
</td>
<td class="quantity">
<input v-model.number="service.quantity" type="number" min="0" />
</td>
<td>
<div class="total">{{ service.total | currency }} €</div>
<div class="tva">+{{ service.total | tva(invoice.tax) }} € ({{ invoice.tax }}%)</div>
</td>
</tr>
每当我更改inputs
service.quantity
or中的值service.price
时,它们都会自动更新,除了service.total
.
解决方案
改用一种方法:
export default {
...
methods: {
getServiceTotal({ price, quantity }) {
return quantity * price;
}
}
...
}
在您的模板中:
<div class="total">{{ getServiceTotal(service) | currency }} €</div>
推荐阅读
- python - 如何计算给定 csv/psv 混合文本文件中的不同字段?
- go - VS Code Go 扩展在保存时损坏文件
- javascript - nodejs router.route 包含所有异步中间件,路由器本身是异步的吗?
- android - 谷歌云视觉对象检测模型在 Android 上崩溃
- maven - 从 Jenkins 中的 maven 获取 project.build.property
- firebase - 如何使用 Dart 在 Flutter 中访问 Firestore clearPersistence() 方法
- android - E/WindowManager: android.view.WindowLeaked: Activity 已泄露窗口 android.widget.PopupWindow$PopupDecorView
- bash - 在 bash 中启动后台进程之间等待
- php - 使用 fopen 在文本多次出现后添加一些文本
- c# - C# MVC 如何使用 TemplateInfo 和 KeyValuePairs 初始化 ViewDataDictionary