1

Problem

I got the following error in my Vue.js + TypeScript project:

<my-input> misses props: value

Component Definition

I followed the Vue.js documentation on the usage of v-model on components as described here.

MyInput.vue

My component for which I want to use the v-model keyword looks like this:

<template>
  <div>
    <input :value="value" @input="$emit('input', $event.target.value)" />
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';

@Component
export default class MyInput extends Vue {
  @Prop({ type: String, required: true }) private readonly value!: string;
}
</script>

MyFirstView.vue

I now want to use myInput component in the following way:

<template>
  <div>
    <my-input v-model="myValue" />
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import MyInput from '@/components/MyInput.vue';

@Component({
  components: { MyInput }
})
export default class MyFirstView extends Vue {
  myValue = 'my test value';
}
</script>

Questions

So accordingly to the official documentation the usage of v-model in my-input resolves to this:

<my-input v-bind:value="myValue" v-on:input="myValue = $event" />

So I obviously set the value property with the v-model keyword, right? But why do I get that error that the value prop wouldn't have been set?

When I change the required keyword to false the error vanishes but this can't be the right solution, right? Because I want the value property to be required.


Used IDE: VSCode with ESLint+Prettier and Vue.js 2.6.11

3
  • I also get this error, but only in VSCode. I don't see this error with npm run build or npm run serve on the command line. Commented Sep 9, 2020 at 11:27
  • @smeier_ec so my code is actually correct? Commented Sep 9, 2020 at 12:03
  • I would say so. I have no idea why VSCode suddenly shows these error messages though (I'm pretty sure it didn't a few weeks ago). Commented Sep 10, 2020 at 13:05

1 Answer 1

2

This was a bug in Vetur, fixed in https://github.com/vuejs/vetur/pull/2241 Update Vetur to v0.27.2 or later.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.