Vue和TypeScript必需的道具

mir*_*ira 4 typescript vue.js vue-component vuejs2

我使用vue-property-decorator,在组件类中添加了必需的道具,但是当我尝试使用没有道具的组件时,我没有看到任何控制台错误,表明缺少必需的道具。为什么?

export default class Test extends Vue {
  @Prop() private message!: string;
}
Run Code Online (Sandbox Code Playgroud)

下面的代码不会产生预期的错误:

<test message="Hello" />
Run Code Online (Sandbox Code Playgroud)

以下代码应导致错误,但不会导致错误:

<test />
Run Code Online (Sandbox Code Playgroud)

ton*_*y19 5

@Prop装饰需要一个PropOptions对象,其中包含一个required带有的默认值属性false。要进行message要求,请required: true@Prop声明中指定:

@Prop({ required: true }) private message!: string;
Run Code Online (Sandbox Code Playgroud)

编辑Vue模板