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

export default class Test extends Vue {
  @Prop() private message!: string;
}

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

<test message="Hello" />

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

<test />

最佳答案

@Prop 装饰器采用 PropOptions 对象,该对象包含默认值为requiredfalse属性。要使message成为必需,请在required: true声明中指定@Prop:

@Prop({ required: true }) private message!: string;

typescript - Vue和TypeScript必需的 Prop-LMLPHP

07-24 18:42
查看更多