本文介绍了Vue 和 TypeScript 所需的道具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 vue-property-decorator
在我的组件类中添加了一个必需的 prop,但是当我尝试使用没有 prop 的组件时,我没有看到任何指示所需 prop 的控制台错误不见了.为什么?
export default class Test extends Vue {@Prop() 私信!:字符串;}
以下代码不会产生预期的错误:
以下代码应该会导致错误,但不会:
解决方案
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 />
解决方案
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;
这篇关于Vue 和 TypeScript 所需的道具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!