我们的 Ember 组件通常使用以下模式,它在 ember-typescript 2 中按预期工作:
export default class DatePicker extends Component {
format: string = this.format || 'dd/LL/yyyy';
}
如果格式未通过模板传递,则使用“dd/LL/yyyy”进行初始化。
使用 ember-typescript 3 和较新的 TS 版本,编译器不再满意。它抱怨
error TS2729: Property 'format' is used before its initialization.
因为它对this.format
.
我应该怎么做才能解决这个问题?我们的代码(到目前为止工作正常)是否无效并且应该更改?如果是如何?
您不需要将其this.format
作为默认值的一部分。做就是了:
export default class DatePicker extends Component {
format: string = 'dd/LL/yyyy';
}
如果您的模板传入@format
,它将覆盖您在组件的 TS 文件中的默认值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句