【问题标题】:Vue.js + Typescript: Why is v-model not recognised as value?Vue.js + Typescript:为什么 v-model 不被识别为值?
【发布时间】: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-modelmy-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 buildnpm run serve 出现此错误。
  • @smeier_ec 所以我的代码实际上是正确的?
  • 我会这么说。我不知道为什么 VSCode 突然显示这些错误消息(我很确定几周前没有)。

标签: typescript vue.js vuejs2


【解决方案1】:

这是 Vetur 中的一个错误,已在 https://github.com/vuejs/vetur/pull/2241 更新 Vetur 到 v0.27.2 或更高版本中修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2022-11-26
    • 2021-09-06
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多