简体   繁体   English

Angular-如何使用Angular CLI创建子组件

[英]Angular - how to create a child component using Angular CLI

I have created an Angular app using Angular CLI and created one component: ng gc parent . 我已经使用Angular CLI创建了一个Angular应用程序,并创建了一个组件: ng gc parent

How do I create child components (eg child1 and child2) and integrate them with a parent using angular CLI? 如何创建子组件(例如child1和child2),并使用角度CLI将它们与父组件集成?

I am trying to create a simple example and understand in some example parent-child component relationship. 我试图创建一个简单的示例,并在一些示例中了解父子组件的关系。

If you mean to nest the components into the parent you can do it like this 如果您打算将组件嵌套到父级中,则可以这样做

ng generate component parent/child1
ng generate component parent/child2

This generates the child components inside the parent folder. 这将在父文件夹内生成子组件。 I do this in my apps to keep together what belongs together! 我在我的应用程序中执行此操作以使所有内容保持在一起! Hope it helped 希望能有所帮助

you can create as many as component you want 您可以创建所需数量的组件

ng g c child1
ng g c child2

Then you need to add them in the Html of ParentComponent 然后,您需要将它们添加到ParentComponent的HTML中

<app-child1></app-child1>
<app-child2></app-child2>

and then using @Input and @Output to pass data between parent and child 然后使用@Input@Output在父级和子级之间传递数据

You can see child routing configuration y child routing component in Angular.io. 您可以在Angular.io中看到子路由配置和子路由组件。

angular.io router angular.io路由器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM