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

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

例如:ng g module newModule

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

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


当前回答

这应该可以工作:

共享/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,否则 它会说这个模块不存在。

其他回答

这对我来说很管用:

1 -->  ng g module new-module

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

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

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

ng g c componentName -m ModuleName

第一个生成模块:

ng g m moduleName --routing

这将创建一个moduleName文件夹,然后导航到module文件夹

cd moduleName

然后生成组件:

ng g c componentName --module=moduleName.module.ts --flat

使用——flat表示不在模块文件夹中创建子文件夹

我使用这个特殊的命令在模块内生成组件。

ng g c <module-directory-name>/<component-name>

该命令将在模块本地生成组件。 或者您可以先输入更改目录。

cd <module-directory-name>

然后创建组件。

ng g c <component-name>

注意:<>中包含的代码表示用户特定的名称。

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

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

然后你的命令会像这样:

ng g c user/userComponent -m user.module