ngforof< string |字符串[],ngiterable< string | string []>>
我有一个数据类型:
export interface TYPE_A {
valueType: TYPE_A_VALUE_TYPES;
value: string | string[];
}
export enum TYPE_A_VALUE_TYPES {
singleValue = "singleValue",
multiValue = "multiValue",
}
并且,我在组件中使用type_a
@input
:
@Input() datas: TYPE_A[] = [];
这是我的html代码:
<div class="feildContainer" *ngFor="let data of datas">
<div class="singleFeild" *ngIf="data.valueType == 'singleValue'">
...
</div>
<div class="multiFeild" *ngIf="data.valueType == 'multiValue'">
<other-component *ngFor="let dataValue of data.value"></other-component>
<div>
</div>
我在vscode :
NgForOf<string | string[], NgIterable<string | string[]>>.ngForOf: NgIterable<string | string[]> | null | undefined
我了解此错误的逻辑,但是我正在寻找解决此问题的最佳解决方案。
I have a data type:
export interface TYPE_A {
valueType: TYPE_A_VALUE_TYPES;
value: string | string[];
}
export enum TYPE_A_VALUE_TYPES {
singleValue = "singleValue",
multiValue = "multiValue",
}
And, I using TYPE_A
in the component for @Input
:
@Input() datas: TYPE_A[] = [];
And this is my HTML code:
<div class="feildContainer" *ngFor="let data of datas">
<div class="singleFeild" *ngIf="data.valueType == 'singleValue'">
...
</div>
<div class="multiFeild" *ngIf="data.valueType == 'multiValue'">
<other-component *ngFor="let dataValue of data.value"></other-component>
<div>
</div>
I getting this error in vscode
:
NgForOf<string | string[], NgIterable<string | string[]>>.ngForOf: NgIterable<string | string[]> | null | undefined
I understand the logic of this error, But I'm looking for the best solution to this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用歧视的联合作为您的类型:
然后,当您要使用
value
项目时,首先对两个可能的值之一检查valueType
,编译器将知道您想参考单个或多估项目,并相应地假设。valueType
value
之后,不会是string |字符串[]
,但是字符串
或string []
,如所检查。Use a discriminated union as your type:
Then, when you want to use the
value
item, you first check thevalueType
against one of the two possible values, and the compiler will know that you want to refer to either the single or multivalued item and assume accordingly. After anif
ofvalueType
value
won't bestring | string[]
, butstring
orstring[]
as checked.