0

Is it possible to at runtime query the declarations of a module class that is decorated with an @NgModule.

@NgModule({
    imports: [
    ...
    ],
    declarations: [
        Component1,
        Component2,
        Component3
    ]
})
export class MyModule { }

So, what I am trying to accomplish is roughly:

const declarations: Type[] = MyModule.declarations;
bvdb
  • 22,839
  • 10
  • 110
  • 123
  • Already answered here https://stackoverflow.com/questions/34465214/access-meta-annotation-inside-class-typescript/34466523#34466523 – Andrea Alhena Mar 25 '19 at 13:06
  • @AndrewReborn thanks for referencing my answer, but I'm afraid that's an old answer. Not sure if that still works. – Poul Kruijt Mar 25 '19 at 13:06

1 Answers1

1

I'm not entirely sure, but I believe in AOT mode this answer will not work. Apart from that, this is a private/naughty access property and subject to change, but you can access the definition like this:

const descriptor = Reflect.getOwnPropertyDescriptor(MyModule, '__annotations__');

if (descriptor) {
  const decorator = descriptor.value && descriptor.value[0];

  if (decorator) {
    const { declarations } = decorator; 
  }
}
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149