我是 vue 的新手,正在处理基于 vue.js 的任务。我正在使用道具在组件中显示我的数据。现在我想添加一种方法来增加产品的数量。
这是我的代码:
<div v-for="(products, index) in products">
<mdc-layout-cell span="2" align="middle">
{{ products.product_barcode }}
</mdc-layout-cell>
<mdc-layout-cell span="2" align="middle">
{{ products.product_quantity}}
</mdc-layout-cell>
<i class="mdc-icon-toggle material-icons float-left"
aria-pressed="false"
v-on:click="incrementItem(index)">
add
</div>
这是我的 JS:
export default {
props: [
'products',
],
methods: {
incrementItem(index) {
let item = this.products[index];
this.products[index].product_quantity =
this.products[index].product_quantity + 1;
console.log(this.products[index].product_quantity);
},
}
我可以在控制台中看到增加的值,但相应行中的值没有增加。我怎样才能增加 product_quantity 的值?任何帮助将不胜感激
原文由 Harris Khan 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以将
props
值分配给变量。然后使用它的子组件。您应该更改
props
子组件中的结构。在子组件中使用newProducts
如果您更新父道具数据,您应该使用emit
用于子到父通信。如需进一步的父子通信,您可以按照本教程进行操作: https ://www.bdtunnel.com/2018/02/vue-js-component-communication-complete.html