Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
floydback opened this issue Nov 19, 2020 · 3 comments · Fixed by #705
Closed

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

floydback opened this issue Nov 19, 2020 · 3 comments · Fixed by #705
Assignees

Comments

@floydback
Copy link

floydback commented Nov 19, 2020

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

@skirtles-code
Copy link
Contributor

I'll put in a PR for this. It looks like a few of the v-model examples need updating.

@skirtles-code skirtles-code self-assigned this Nov 20, 2020
@skirtles-code
Copy link
Contributor

There are some tweaks needed in the docs and I've got a first draft of those locally. However, there's something more going on here.

Most of the documentation examples do not show a warning when run in isolation, even though they don't include emits. The warning isn't supposed to be shown when emits isn't specified. It should only be shown when it is specified but an event is missing.

I did a bit of digging and came up with this test case:

https://jsfiddle.net/skirtle/yofbtL0h/

Notice this line:

app.mixin({})

With that line included it shows the warning. Without that line the warning goes away.

The global mixin triggers a merge operation, leading to an emits that's an empty object. That's enough to trigger the relevant check, leading to the warning.

I need to give this some more thought but I'll probably end up reporting it as a library bug.

@floydback Do you have any global mixins in your application, consistent with my theory?

@floydback
Copy link
Author

Do you have any global mixins in your application, consistent with my theory?

Yes! I can confirm this. Right now I've remove a plugin (it add global mixin) and this warning go away.

I also tried to reproduce this warning but it didn't show up on a pure project. I could not understand what is the reason. I think your theory is right. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants