【发布时间】:2021-06-18 14:49:50
【问题描述】:
我想在一个组件上添加一个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>
我试图reproduce this tutorial,但我卡住了,不知道我错过了什么。
我想在 Parent.vue 组件中显示{{ name }}。你知道如何解决这个问题吗?
【问题讨论】:
标签: vue.js vuejs3 vue-composition-api v-model vue-script-setup