简体   繁体   English

如果使用[] .concat()组合多个数组,Angular @NgModule导入不会看模块

[英]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: 但是如果使用concat方法它不起作用,如果使用下一个方法,任何组件将无法工作:

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

What difference for typescript compiler for this both methods? 这两种方法的typescript编译器有什么区别? Cause if we see 因为我们看到了

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

Both results looks the same 两种结果看起来都一样

It's difficult to answer this question, because this aspect of the Angular compiler doesn't have much documentation. 回答这个问题很难,因为Angular编译器的这方面没有太多文档。 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. 引擎盖下 ,AOT编译器重写了装饰器使用的大量元数据,我怀疑这是一个无法正确转换的例子。

I can tell you that this has nothing to do with TypeScript or JavaScript. 我可以告诉你,这与TypeScript或JavaScript无关。 It is a problem with the AOT compiler. 这是AOT编译器的问题。

You can read about transformers in this blog: 您可以在此博客中阅读有关变形金刚的内容:

https://blog.angularindepth.com/do-you-know-how-angular-transforms-your-code-7943b9d32829 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 https://github.com/angular/angular/tree/master/packages/compiler-cli/src/transformers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM