angular7 aot:不支持表达式

angular7 aot: Expression form not supported

在 Angular7 中,我想像这样创建一个实用程序 class:

export class FieldNameUtils {

  static toHump(name: string) {
    return name.replace(/_(\w)/g, function (all, letter) {
      return letter.toUpperCase();
    });
  }

  static toLine(name: string) {
    return name.replace('/([A-Z])/g', '_').toLowerCase();
  }
}

它在开发模式下运行良好,但是当我在生产模式下构建时,出现如下错误:

field.name.utils.ts:4:25: Metadata collected contains an error that will be reported at runtime: Expression form not supported.
  {"__symbolic":"error","message":"Expression form not supported","line":3,"character":24}

我尝试将 /_(\w)/g 更改为静态变量,但仍然出现错误,我知道不支持 lambda 表达式,所以我已经将 (all, letter) => ... 更改为 function(all, letter){ ... } 报错显示不支持replace(后的表达式,而是RegExp

您是否尝试过如下设置您的正则表达式?

const regex = new RegExp('_(\w)', 'g');