提问者:小点点

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


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

有没有一种方法可以为一个新模块创建一个组件?

例如:ng g module newmodule

NG g component NewComponent(如何将此组件添加到NewModule??)

因为angular-cli的默认行为是将所有新组件放入app.module中。我想选择组件所在的位置,这样我就可以创建独立的模块,而不会将所有组件都放在app.module中。是否可以使用angular-cli完成此操作,还是必须手动完成此操作?


共2个答案

匿名用户

要将组件创建为模块的一部分,您应该

  1. ng g module newmodule生成模块,
  2. cd newmodule将目录更改为newmodule文件夹
  3. ng g component newcomponent将组件创建为模块的子组件。

更新:Angular 9

现在,在生成组件时,您在哪个文件夹中已经不重要了。

  1. ng g module newmoudle生成模块。
  2. 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