【发布时间】:2020-09-30 11:04:31
【问题描述】:
我正在使用 typescript 使用 stenciljs,我的组件可以在任何地方使用,并且用户可以在字符串化数组或普通数组中发送数据,例如如下
<my-component data ="JSON.stringify([1,3,4]") />
用户也可以像下面这样传递普通数组:
<my-component data=[1,4,5] />
所以在我的模板组件中,我有以下内容。
export class Charts {
@Prop() data: string;
@Prop() chartTitle: string;
@State() parsedData= [];
componentWillLoad() {
try {
this.parsedData = JSON.parse(this.data);
} catch (error) {
this.parsedData = this.data || []; // here it gives me error Type 'string | undefined[]' is not assignable to type 'any[]'.
Type 'string' is not assignable to type 'any[]'
}
}
}
我在 cmets 中提供了错误,请参考上面的代码,如果我使 @State:parsedData:any=[]; 有效,但我不想在这里使用任何东西,有什么帮助吗?
【问题讨论】:
标签: typescript