【问题标题】:ng build gives an error because of regExp由于 regExp,ng build 给出错误
【发布时间】:2018-07-22 21:22:18
【问题描述】:

我在常量中有 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

有什么想法吗?

【问题讨论】:

  • 问题可能出在您使用它的方式上,而不是您定义它的方式上。从您的 AppModule 发布代码。还要发布您正在执行的确切命令(例如 ng buildng build --prod)。

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


【解决方案1】:

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);

这样,您仍然可以传递正则表达式的含义,并在运行时需要它们时即时创建一个

【讨论】:

  • 我在使用 text-mask 库时遇到了同样的问题。我需要提供一个正则表达式数组,例如 [/\d/, /\d/, '.',/\d/] 但如果我使用 [ new RegExp('\\d/'), new RegExp('\ \d/'), '.', new RegExp('\\d/') ] 不行...:(
  • 我有同样的问题 /translate3d(\s?(?[-]?\d*)px,\s?(?[-]?\d*) px,\s?(?[-]?\d*)px)/ 你能帮我吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
相关资源
最近更新 更多