【发布时间】:2020-09-08 15:00:06
【问题描述】:
问题
我的 Vue.js + TypeScript 项目出现以下错误:
<my-input> misses props: value
组件定义
我按照 Vue.js 文档了解了 v-model 在组件上的用法,如 here 所述。
MyInput.vue
我想要使用v-model 关键字的组件如下所示:
<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
我现在想通过以下方式使用myInput 组件:
<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>
问题
因此,根据官方文档,v-model 在my-input 中的使用解决了这个问题:
<my-input v-bind:value="myValue" v-on:input="myValue = $event" />
所以我显然用v-model 关键字设置了value 属性,对吧?但是为什么我会收到 value 属性未设置的错误?
当我将 required 关键字更改为 false 时,错误消失了,但这不是正确的解决方案,对吧?因为我希望 value 属性是必需的。
使用的 IDE:带有 ESLint+Prettier 和 Vue.js 2.6.11 的 VSCode
【问题讨论】:
-
我也收到此错误,但仅在 VSCode 中。我在命令行上没有看到
npm run build或npm run serve出现此错误。 -
@smeier_ec 所以我的代码实际上是正确的?
-
我会这么说。我不知道为什么 VSCode 突然显示这些错误消息(我很确定几周前没有)。
标签: typescript vue.js vuejs2