ng build由于regExp而报错

Mom*_*men 2 regex typescript angular-cli angular2-aot angular

我在常量中有regExp:

export const Constants = {
    QUERY_PARAM_CONFIG: {
        REGEX: /\+/gi,
        REGEX_ENCODED: /%2B/g
    }
};
Run Code Online (Sandbox Code Playgroud)

但是当我运行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.
Run Code Online (Sandbox Code Playgroud)

npm构建脚本:

ng build --prod --output-hashing none --sourcemaps=true
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

smn*_*brv 7

AOT不支持正则表达式文字。请参阅支持的语法列表

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

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

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

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