ng build 由于 regExp 而给出错误

ng build gives an error because of regExp

我有 regExp 常量:

export const Constants = {
    QUERY_PARAM_CONFIG: {
        REGEX: /\+/gi,
        REGEX_ENCODED: /%2B/g
    }
};

但是当我 运行 ng build 我得到一个错误:

ERROR in app\app.module.ts(58,21): Error during template compile of 'AppModule'
  Expression form not supported in 'Constants'
    'Constants' contains the error at app\configuration\constants.ts.

npm 构建脚本:

ng build --prod --output-hashing none --sourcemaps=true

有什么想法吗?

AOT 不支持正则表达式文字。见 list of supported syntax.

为了使其正常工作,请将您的正则表达式转换为受支持的内容,例如字符串:

const before = /\+/gi, // before, could be the same as below

const after = { re: '\+', flags: 'gi' }
const canBeUsedAs = new RegExp(after.re, after.flags);

通过这种方式,您仍然可以传递 RegExp 的含义,并在运行时需要时即时创建一个