typescript - Vue 和 TypeScript 需要 prop

标签 typescript vue.js vuejs2 vue-component

我使用 vue-property-decorator 向我的组件类添加了一个必需的 prop,但是当我尝试在没有该 prop 的情况下使用该组件时,我没有看到任何指示所需 prop 的控制台错误不见了。为什么?

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

以下代码没有产生预期的错误:

<test message="Hello" />

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

<test />

最佳答案

@Prop装饰者需要一个 PropOptions对象,它包含一个默认值为 falserequired 属性。要使 message 成为必需,请在 @Prop 声明中指定 required: true:

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

Edit Vue Template

关于typescript - Vue 和 TypeScript 需要 prop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56801082/

相关文章:

javascript - VueJS : Custom Socket. io 发射在触发时未得到处理

javascript - 如何制作一个像 Typescript 中真正的 "If"一样工作的 React "if"组件?

reactjs - typescript 错误 : Type '{ onClick: () => void; }' is not assignable to type 'IntrinsicAttributes'

vue.js - 如何在单击按钮时关闭 Vuesax PopUp 屏幕?

javascript - 有没有办法让 v-if 在 v-for 中工作来动态渲染模板来创建子表?

javascript - vue + quill - 图标显示为文本(由文件加载器引起)

jquery - 错误 : Property 'draggable' does not exist on type 'JQuery<HTMLElement>'

typescript - 将 JSON 作为类型导入

environment-variables - 将环境变量加载到 vue.js 中

javascript - Vue.js - Prop 未定义