Skip to content

Why v-model on the сustom component throw a warning without emits? #702

Closed
@floydback

Description

@floydback

According to the docs this code should be enough
MyCopmponent.vue

<template>
  <button @click="update">+1</button>
</template>

<script>
export default {
  name: 'MyComponent',

  // emits: ['update:modelValue'], <--- without this I got a warning

  props: {
    modelValue: {
      type: Number,
      required: true,
      default: 0
    }
  },

  methods: {
    update () {
      this.$emit('update:modelValue', this.modelValue + 1)
    }
  }
}
</script>

but every time I change modelValue I got warning
[Vue warn]: Component emitted event "update:modelValue" but it is neither declared in the emits option nor as an "onUpdate:modelValue" prop. runtime-core.esm-bundler.js:156

Adding emits: ['update:modelValue'] to the component solve this, but is this behavior correct? I didn't find in the docs adding this instruction is necessary.

I use @vue/cli 4.5.8 and vue 3.0.2

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions