用Angular-CLI创建特定模块的组件

我开始使用angular-cli,我已经阅读了很多关于我想做的事情的答案......没有成功,所以我来到这里。

有什么方法可以为新模块创建一个组件吗?

比如说。ng g module newModule

ng g component newComponent(如何将这个组件添加到newModule?)

因为angular-cli的默认行为是把所有的新组件放在app.module里。我想选择我的组件的位置,这样我就可以创建分离的模块,而不会让我的所有组件都在app.module里。是否可以用angular-cli来做,还是我必须手动做?

解决办法

要创建一个组件作为模块的一部分,你应该

1.ng g module newModule来生成一个模块。 2.cd newModule改变目录到newModule文件夹中。 3.3. ng g component newComponent创建一个组件作为模块的子模块。

评论(9)
ng g component nameComponent --module=app.module.ts
评论(1)

不确定也许Alexander Ciesielski'的答案在写的时候是否正确,但我可以证实这不再是工作。你在项目中的哪个目录下运行Angular CLI并不重要。如果你输入

ng g component newComponent

就会生成一个组件并将其导入app.module.ts文件中。

你可以使用CLI将其自动导入到另一个模块中的唯一方法是,指定

ng g component moduleName/newComponent

其中moduleName是你在项目中已经定义的模块。如果moduleName不存在,它将把组件放在moduleName/newComponent目录中,但仍将其导入app.module中。

评论(5)