【发布时间】:2019-04-19 04:47:48
【问题描述】:
我有一个文件,其中包含我的所有组件,如下所示:
components.ts
export { ComponentA } from '../some/path/a.component';
export { ComponentB } from '../some/path/b.component';
export { ComponentC } from '../some/path/c.component';
// ... more here
在我的模块文件中,我有这个:
components.module.ts
import { ComponentA } from '../some/path/components';
import { ComponentB } from '../some/path/components';
import { ComponentC } from '../some/path/components';
// ... more here
@NgModule({
declarations: [ComponentA, ComponentB, ComponentC]
})
export class ComponentsModule { }
有没有办法我可以只做import * as Components from '../some/path/components' 并将其放入declarations 数组中?我有很多组件,列表可能会变得很大。此外,每次我添加一个新组件时,我都必须更新这两个文件,如果我可以将它作为导出添加到 components.ts 文件中并且import * 会处理其中的任何新组件会更好模块。
如果有人知道,将不胜感激。或者我什至应该这样做吗?如果没有,什么是好的做法?
我尝试这样做,但它返回所有组件的构造函数列表,当我尝试构建它时,Angular 抱怨它无法确定每个组件的模块。
import * as exports from '../some/path/components';
const Components: any[] = Object.keys(exports).map(key => exports[key]);
...
delcarations: [Components]
...
【问题讨论】:
标签: angular typescript