使用Angular-CLI为特定模块创建组件 [英] Create component to specific module with Angular-CLI

查看:401
本文介绍了使用Angular-CLI为特定模块创建组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm starting to use angular-cli and I've already read a lot to find an answer about what I want to do...no success, so I came here.

有没有办法为新模块创建组件?

Is there a way to create a component to a new module?

例如:ng g module newModule

ng g component newComponent(如何将此组件添加到newModule?)

ng g component newComponent (how to add this component to newModule??)

因为angular-cli的默认行为是将所有新组件放入app.module中.我想选择组件的位置,以便可以创建单独的模块,而不会在app.module中包含所有组件.可以使用angular-cli来做到这一点,还是必须手动执行此操作?

because the angular-cli default behavior is to put all new components inside app.module. I would like to choose where my component will be, so that I can create separated modules and won't have all my components inside app.module . It is possible to do that using angular-cli or do I have to do this manually?

推荐答案

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

To create a component as part of a module you should

  1. ng g module newModule生成模块,
  2. cd newModule将目录更改为newModule文件夹
  3. ng g component newComponent创建一个组件作为模块的子代.
  1. ng g module newModule to generate a module,
  2. cd newModule to change directory into the newModule folder
  3. ng g component newComponent to create a component as a child of the module.

更新:Angular 9

UPDATE: Angular 9

现在生成组件时所处的文件夹都没有关系.

Now it doesn't matter what folder you are in when generating the component.

  1. ng g module NewMoudle生成模块.
  2. ng g component new-module/new-component创建NewComponent.
  1. ng g module NewMoudle to generate a module.
  2. ng g component new-module/new-component to create NewComponent.

注意:当Angular CLI看到new-module/new-component时,它将理解并转换大小写以匹配new-module-> NewModule和new-component-> NewComponent.一开始可能会引起混淆,所以简单的方法是将#2中的名称与模块和组件的文件夹名称匹配.

Note: When the Angular CLI sees new-module/new-component, it understands and translates the case to match new-module -> NewModule and new-component -> NewComponent. It can get confusing in the beginning, so easy way is to match the names in #2 with the folder names for the module and component.

这篇关于使用Angular-CLI为特定模块创建组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆