使用Angular-CLI为特定模块创建组件 [英] Create component to specific module with Angular-CLI
问题描述
我开始使用angular-cli,并且我已经读了很多书来找到关于我想做什么的答案...没有成功,所以我来到了这里.
I'm starting to use angular-cli and I've already read a lot to find an answer about what I want to do...no success, so I came here.
有没有办法为新模块创建组件?
Is there a way to create a component to a new module?
例如:ng g module newModule
ng g component newComponent
(如何将此组件添加到newModule?)
ng g component newComponent
(how to add this component to newModule??)
因为angular-cli的默认行为是将所有新组件放入app.module
中.我想选择组件的位置,以便可以创建单独的模块,而不会在app.module
中包含所有组件.可以使用angular-cli来做到这一点,还是必须手动执行此操作?
because the angular-cli default behavior is to put all new components inside app.module
. I would like to choose where my component will be, so that I can create separated modules and won't have all my components inside app.module
. It is possible to do that using angular-cli or do I have to do this manually?
推荐答案
要将组件创建为模块的一部分,您应该
To create a component as part of a module you should
-
ng g module newModule
生成模块, -
cd newModule
将目录更改为newModule
文件夹 -
ng g component newComponent
创建一个组件作为模块的子代.
ng g module newModule
to generate a module,cd newModule
to change directory into thenewModule
folderng g component newComponent
to create a component as a child of the module.
更新:Angular 9
UPDATE: Angular 9
现在生成组件时所处的文件夹都没有关系.
Now it doesn't matter what folder you are in when generating the component.
-
ng g module NewMoudle
生成模块. -
ng g component new-module/new-component
创建NewComponent.
ng g module NewMoudle
to generate a module.ng g component new-module/new-component
to create NewComponent.
注意:当Angular CLI看到new-module/new-component时,它将理解并转换大小写以匹配new-module-> NewModule和new-component-> NewComponent.一开始可能会引起混淆,所以简单的方法是将#2中的名称与模块和组件的文件夹名称匹配.
Note: When the Angular CLI sees new-module/new-component, it understands and translates the case to match new-module -> NewModule and new-component -> NewComponent. It can get confusing in the beginning, so easy way is to match the names in #2 with the folder names for the module and component.
这篇关于使用Angular-CLI为特定模块创建组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!