Vue and TypeScript required prop

旧城冷巷雨未停 提交于 2020-03-22 04:06:33

问题


I added a required prop to my component class using vue-property-decorator, but when I tried using the component without the prop, I didn't see any console errors that indicate the required prop is missing. Why?

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

The following code yields no errors as expected:

<test message="Hello" />

The following code should result in an error but doesn't:

<test />

回答1:


The @Prop decorator takes a PropOptions object, which contains a required property with a default value of false. To make message required, specify required: true in your @Prop declaration:

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



来源:https://stackoverflow.com/questions/56801082/vue-and-typescript-required-prop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!