如何实现Vue子组件向父组件传递多个值并修改其内容?
- 内容介绍
- 文章标签
- 相关推荐
本文共计705个文字,预计阅读时间需要3分钟。
1. 子组件向父组件传递一个值: 子组件:`this.$emit('change', this.value);`
2. 父组件接收子组件的值并使用: 父组件:`!-- 在父组件中使用子组件 -- editable-cell :text=text :inputType=inputType @change=costPlannedAmountChange($event) / / / 事件`
一、子组件向父组件传递一个值
子组件:
this.$emit('change', this.value);
父组件:
<!-- 在父组件中使用子组件 --> <editable-cell :text="text" :inputType="inputType" @change="costPlannedAmountChange($event)" />
// 事件处理函数 async costPlannedAmountChange(value) { console.log(value) }
在使用子组件时,绑定change函数的事件处理函数也可以写成如下格式:
<editable-cell :text="text" :inputType="inputType" @change="costPlannedAmountChange" />
绑定事件处理函数时,可以不带括号,形参则默认为事件对象,如果绑定时带上了括号,再想使用事件对象则需要传入$event作为实参。
本文共计705个文字,预计阅读时间需要3分钟。
1. 子组件向父组件传递一个值: 子组件:`this.$emit('change', this.value);`
2. 父组件接收子组件的值并使用: 父组件:`!-- 在父组件中使用子组件 -- editable-cell :text=text :inputType=inputType @change=costPlannedAmountChange($event) / / / 事件`
一、子组件向父组件传递一个值
子组件:
this.$emit('change', this.value);
父组件:
<!-- 在父组件中使用子组件 --> <editable-cell :text="text" :inputType="inputType" @change="costPlannedAmountChange($event)" />
// 事件处理函数 async costPlannedAmountChange(value) { console.log(value) }
在使用子组件时,绑定change函数的事件处理函数也可以写成如下格式:
<editable-cell :text="text" :inputType="inputType" @change="costPlannedAmountChange" />
绑定事件处理函数时,可以不带括号,形参则默认为事件对象,如果绑定时带上了括号,再想使用事件对象则需要传入$event作为实参。

