Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular @NgModule imports don`t see modules if use a [].concat() for combine several arrays

I have a lot of modules and want to sort and split it by several arrays by types

const aModules: any[] = [
  Module1,
  Module2,
  Module3,
  Module4
];

const bModules: any[] = [
  Module5,
  Module6,
  Module7,
  Module8
]

It will works if use a spread operator

imports: [Module0, ...aModules, ...bModules]

But it not works if use a concat method, any components will not works if use next approach:

imports: [Module0].concat(aModules, bModules)

What difference for typescript compiler for this both methods? Cause if we see

console.log([Module0, ...aModules, ...bModules]);
console.log([Module0].concat(aModules, bModules));

Both results looks the same

like image 446
Maksym Petrenko Avatar asked Aug 06 '19 19:08

Maksym Petrenko


People also ask

What does @NgModule do in Angular?

@NgModule takes a metadata object that describes how to compile a component's template and how to create an injector at runtime. It identifies the module's own components, directives, and pipes, making some of them public, through the exports property, so that external components can use them.

Which Angular module automatically imports by BrowserModule the imported module has directives available like ngIf & ngFor?

BrowserModule and CommonModule link BrowserModule imports CommonModule , which contributes many common directives such as ngIf and ngFor . Additionally, BrowserModule re-exports CommonModule making all of its directives available to any module that imports BrowserModule .

What should be imported BrowserModule or CommonModule?

Should I import BrowserModule or CommonModule ? link. The root application module, AppModule , of almost every browser application should import BrowserModule from @angular/platform-browser . BrowserModule provides services that are essential to launch and run a browser application.


1 Answers

It's difficult to answer this question, because this aspect of the Angular compiler doesn't have much documentation. Under the hood the AOT compiler rewrites a lot of the metadata used by the decorators, and I would suspect this is an example of something it can not transform correctly.

I can tell you that this has nothing to do with TypeScript or JavaScript. It is a problem with the AOT compiler.

You can read about transformers in this blog:

https://blog.angularindepth.com/do-you-know-how-angular-transforms-your-code-7943b9d32829

You can find the source code to the transformers here:

https://github.com/angular/angular/tree/master/packages/compiler-cli/src/transformers

like image 120
Reactgular Avatar answered Nov 15 '22 05:11

Reactgular