【问题标题】:Operator '===' cannot be applied to types 'number' and 'string .. when used in [ngClass]在 [ngClass] 中使用时,运算符 '===' 不能应用于类型 'number' 和 'string ..
【发布时间】:2018-09-04 01:10:03
【问题描述】:

当变量cfgSelected 具有特定值时,我需要一个 div 来拥有一个 css 类 .. 这里是 '1' (

<div [ngClass]="(cfgSelected ==='1')?'isActive':'isNormal'" 
     (click)="selectThisOne('1')">
bla ...
</div>

在 ts 文件中:

selectThisOne(id: any) {
    this.cfgSelected = id;
}

它在 --dev 中工作,但是当我想构建 --prod 它时,我得到了

src\app\configurator\configurator.component.html(80,50) 中的错误::运算符 '===' 不能应用于类型 'number' 和 'string'。

谁能告诉我这有什么问题?

【问题讨论】:

  • 改用整数,或者至少解析它。
  • 补充一点,=== 运算符不会将参数转换为相同的类型,但如果您使用 == 它可以工作(但不是最佳实践)

标签: css angular typescript ng-class


【解决方案1】:

(cfgSelected ==='1') 更改为(cfgSelected === 1)

原因

因为prod 会进行类型检查?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-28
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多