创建组件 &使用 Angular-CLI 将其添加到特定模块 [英] Create component & add it to a specific module with Angular-CLI

查看:42
本文介绍了创建组件 &使用 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天全站免登陆