自定义表单组件怎么把值传回父页面并获取
时间: 2024-03-12 15:48:02 浏览: 21
在自定义表单组件中,可以使用 `this.$emit()` 方法向父组件传递值。例如,在组件中有一个输入框,当输入框的值发生改变时,可以通过以下方式将值传递给父组件:
```javascript
<template>
<div>
<input v-model="inputValue" @input="inputChange" />
</div>
</template>
<script>
export default {
data() {
return {
inputValue: ''
}
},
methods: {
inputChange() {
this.$emit('input-change', this.inputValue)
}
}
}
</script>
```
在父组件中,可以通过监听 `input-change` 事件来获取子组件传递的值:
```javascript
<template>
<div>
<custom-form @input-change="handleInputChange" />
</div>
</template>
<script>
import CustomForm from './CustomForm.vue'
export default {
components: {
CustomForm
},
methods: {
handleInputChange(value) {
// 在这里处理子组件传递的值
}
}
}
</script>
```
在父组件中引入自定义表单组件后,可以通过 `@input-change` 监听子组件传递的值,并在 `handleInputChange` 方法中对值进行处理。