我想在一个组件上添加一个 v-model
但我收到了这个警告:
[Vue warn]: Component emitted event "input" but it is neither declared in the emits option nor as an "onInput" prop.
这是我的代码:
// Parent.vue
<template>
<h2>V-Model Parent</h2>
<Child v-model="name" label="Name" />
<p>{{ name }}</p>
</template>
<script setup>
import { ref } from 'vue'
import Child from './Child.vue'
const name = ref('')
</script>
// Child.vue
<template>
<input
class="input"
type="text"
:placeholder="props.label"
:value="props.value"
v-on:input="updateValue($event.target.value)"
/>
</template>
<script setup>
import { defineProps, defineEmit } from 'vue'
const props = defineProps({
label: String,
value: String
})
const emit = defineEmit('input')
function updateValue(value) {
emit('input', value)
}
</script>
我试图 重现本教程,但我卡住了,不知道我错过了什么。
我想在 Parent.vue 组件中显示 {{ name }}
。你知道如何解决这个问题吗?
原文由 wittgenstein 发布,翻译遵循 CC BY-SA 4.0 许可协议
In vue 3
value
prop has been changed tomodelValue
and the emitted eventinput
toupdate:modelValue
: