我开始使用angular-cli,我已经读了很多关于我想做什么的答案...没有成功,所以我来到这里。
有没有一种方法可以为一个新模块创建一个组件?
例如:ng g module newmodule
NG g component NewComponent
(如何将此组件添加到NewModule??)
因为angular-cli的默认行为是将所有新组件放入app.module
中。我想选择组件所在的位置,这样我就可以创建独立的模块,而不会将所有组件都放在app.module
中。是否可以使用angular-cli完成此操作,还是必须手动完成此操作?
要将组件创建为模块的一部分,您应该
ng g module newmodule
生成模块,cd newmodule
将目录更改为newmodule
文件夹ng g component newcomponent
将组件创建为模块的子组件。更新:Angular 9
现在,在生成组件时,您在哪个文件夹中已经不重要了。
ng g module newmoudle
生成模块。NG g component new-module/new-component
创建新组件。注意:当Angular CLI看到new-module/new-component时,它会理解并转换案例以匹配new-module->NewModule和new-component->newcomponent。它可能会在一开始变得混乱,所以简单的方法是将#2中的名称与模块和组件的文件夹名称匹配。
ng g component nameComponent --module=app.module.ts