由于regExp,ng build会出错

时间:2018-02-12 16:23:49

标签: regex angular typescript angular-cli angular2-aot

我在常量中有regExp:

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

但是当我运行构建时,我得到一个错误:

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 build script:

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

任何想法?

1 个答案:

答案 0 :(得分:6)

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的含义,并在运行时随时创建一个