【问题标题】:how to fix this condition will always return false in Angular如何修复这种情况在 Angular 中总是返回 false
【发布时间】:2020-05-30 02:54:01
【问题描述】:

当我尝试查看 charTypeEnum 是否等于 ChartType.text 时,任何人都知道我为什么会得到“此条件将始终返回 'false',因为类型 'typeof ChartType' 和 'ChartType' 没有重叠”

图表设计.ts

import { ChartType, ChartData, TextData } from 'src/app/mam-chart/models';

export class ChartDesignComponent implements OnInit {
  chartTypeEnum = ChartType;
}

  setupTypes() {
// I got error over here
    if (this.chartTypeEnum == ChartType.text)  { 
      this.cardJsonV2 = JSON.stringify(this.chartData);
    }
}

mam-model.ts

export enum ChartType {
    chart = 1,
    text,
    grid
}

图表设计.html

            <mat-form-field appearance="fill">
                <mat-label>Text</mat-label>
                <input matInput [ngModel]="chartData?.textData?.text" />
            </mat-form-field >

【问题讨论】:

    标签: html angular typescript angular-material


    【解决方案1】:

    尝试将chartTypeEnum = ChartType 替换为chartTypeEnum: ChartType

    对于chartTypeEnum = ChartType,类型ChartType(错误地?)分配给变量chartTypeEnum,而不是定义类型。

    【讨论】:

      【解决方案2】:

      使用类型转换

       if (String(this.chartTypeEnum) === ChartType.text))  { 
      

      【讨论】:

      • 您好,感谢您的回答,但现在我得到“ if (String(this.chartTypeEnum) == ChartType.text)) ”.. 你知道我为什么会得到这个。
      • 如果你在做检查应该是 ===
      猜你喜欢
      • 1970-01-01
      • 2019-09-26
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2016-03-02
      • 2018-04-26
      • 2017-04-29
      相关资源
      最近更新 更多