我开始使用angular-cli,我已经阅读了很多关于我想做什么的答案…没有成功,所以我来到这里。
是否有一种方法可以为新模块创建组件?
例如:ng g module newModule
如何将该组件添加到newModule中??
因为angular-cli的默认行为是把所有新组件放到app.module中。我想选择我的组件将在哪里,这样我就可以创建单独的模块,而不会有我所有的组件在app.module。这是可能的使用angular-cli或我必须手动这样做?
我开始使用angular-cli,我已经阅读了很多关于我想做什么的答案…没有成功,所以我来到这里。
是否有一种方法可以为新模块创建组件?
例如:ng g module newModule
如何将该组件添加到newModule中??
因为angular-cli的默认行为是把所有新组件放到app.module中。我想选择我的组件将在哪里,这样我就可以创建单独的模块,而不会有我所有的组件在app.module。这是可能的使用angular-cli或我必须手动这样做?
当前回答
我使用这个特殊的命令在模块内生成组件。
ng g c <module-directory-name>/<component-name>
该命令将在模块本地生成组件。 或者您可以先输入更改目录。
cd <module-directory-name>
然后创建组件。
ng g c <component-name>
注意:<>中包含的代码表示用户特定的名称。
其他回答
首先运行ng g module newModule . 然后执行ng g component newModule/newModule——flat命令
ng g component nameComponent --module=app.module.ts
阅读描述——route https://angular.io/cli/generate#module-command,
为了存档,您必须将该组件模块的路由添加到某个位置,并指定路由名称。
ng generate module component-name --module=any-parent-module --route=route-path
不确定Alexander Ciesielski的答案在撰写本文时是否正确,但我可以证实这不再适用。不管你在项目的哪个目录下运行Angular CLI。如果你输入
ng g component newComponent
它将生成一个组件并将其导入到app.module.ts文件中
使用CLI自动将其导入到另一个模块的唯一方法是指定
ng g component moduleName/newComponent
其中moduleName是您已经在项目中定义的模块。如果moduleName不存在,它会把组件放在moduleName/newComponent目录下,但仍然会把它导入app.module
我不知道这是不是把你们带到这里来的原因,但我想创建一个包含模块、路由和组件文件的文件夹。我希望将组件声明给我创建的模块。
为此,我发现这个命令非常有用。
Ng路径/to/folder/component—路由路径/to/folder/component
这将创建一个包含6个文件的文件夹。
CREATE path/to/folder/component/component-routing.module.ts (253 bytes)
CREATE path/to/folder/component/component.module.ts (297 bytes)
CREATE path/to/folder/component/component.component.html (26 bytes)
CREATE path/to/folder/component/component.component.spec.ts (655 bytes)
CREATE path/to/folder/component/component.component.ts (295 bytes)
CREATE path/to/folder/component/component.component.scss (0 bytes)
UPDATE path/to/folder/component/component.module.ts (379 bytes)
如果你不想要路由,你可以删除。