MayI
MayI

Reputation: 53

angular7 aot: Expression form not supported

In Angular7 I would like to create a util class like this:

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', '_$1').toLowerCase();
  }
}

It's working well with development mode, but when I build with production, I got error like this:

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}

I have tried to change the /_(\w)/g to a static variable, but still got error, I know lambda expression is not supported, so I'v already change (all, letter) => ... to a function(all, letter){ ... } The error shows that an expression after replace( is not supported, but it is a RegExp

Upvotes: 2

Views: 1653

Answers (2)

IncogVito
IncogVito

Reputation: 71

Try to mark the class as

// @dynamic
export class YourUtilClass {
}

I've encountered this problem during working with angular libraries.

Upvotes: 1

Nico
Nico

Reputation: 179

Have you tried setting up your regex as follows?

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

Upvotes: 6

Related Questions