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

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

例如:ng g module newModule

如何将该组件添加到newModule中??

因为angular-cli的默认行为是把所有新组件放到app.module中。我想选择我的组件将在哪里,这样我就可以创建单独的模块,而不会有我所有的组件在app.module。这是可能的使用angular-cli或我必须手动这样做?


当前回答

对于Angular v4及以上版本,只需使用:

ng g c componentName -m ModuleName

其他回答

您可以尝试下面的命令,该命令描述了

ng -> Angular 
g  -> Generate
c  -> Component
-m -> Module 

然后你的命令会像这样:

ng g c user/userComponent -m user.module

这对我来说很管用:

1 -->  ng g module new-module

2 -->  ng g c new-module/component-test --module=new-module/new-module.module.ts

如果你想生成一个没有目录的组件,使用——flat标志。

ng g component nameComponent --module=app.module.ts

对于Angular v4及以上版本,只需使用:

ng g c componentName -m ModuleName

这应该可以工作:

共享/components/info-card——module=/src/app/shared/shared.module.ts

注:

Ng g c等价于Ng generate component Shared /components/info-card在我项目的src/app中 目录&这是我如何使用它来创建一个组件 在共享/components目录中输入InfoCardComponent (没有指定src/app)。 但是,要将这个新创建的组件添加到特定的模块,您需要 需要在模块的路径中包含/src/app,否则 它会说这个模块不存在。