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

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

例如:ng g module newModule

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

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


当前回答

根据Angular文档,为特定模块创建组件的方法是:

ng g component <directory name>/<component name>

"目录名" = CLI生成特性模块的目录

例子:-

ng generate component customer-dashboard/CustomerDashboard

这将在customer-dashboard文件夹中为新组件生成一个文件夹,并使用CustomerDashboardComponent更新特性模块

其他回答

我用特定的根文件夹创建了基于组件的子模块

下面是我指定的cli命令,请检查

ng g c Repair/RepairHome -m Repair/repair.module

Repair是子模块的根文件夹

-m是——module C代表复合 G代表生成

首先,你必须使用以下命令创建一个新模块:

ng g module newModule

然后,你必须使用以下命令进入该目录:

cd command  
cd newModule

现在你可以继续使用下面的命令:

ng g component newComponent

这个组件会在那个模块中创建。

使用这个简单的命令:

ng g c users/userlist

users:您的模块名。

userlist:你的组件名。

我没有找到一个答案,说明如何使用cli在顶级模块文件夹中生成组件,并让组件自动添加模块的声明集合。

要创建模块,运行以下命令:

ng g module foo

在foo module文件夹中创建组件,并将其添加到foo.module中。Ts的声明集合运行如下:

ng g component foo/fooList --module=foo.module.ts

cli会像这样构建模块和组件:

——编辑angular cli的新版本。1.5.5不需要模块文件名,所以v1.5.5的命令应该是

ng g component foo/fooList --module=foo

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

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

然后你的命令会像这样:

ng g c user/userComponent -m user.module