Angular 什么时候创建组件实例

When does Angular create component instances

我是 angular 的新手,从文档中我了解到我们可以通过 bootstrap 和 root 模块启动 angular 应用程序,然后 angular 可以通过创建 app component 的实例来启动应用程序,并在 index.html 中找到此组件的 selector 并放置该根组件的视图, 我的问题是 angular 是否对所有组件使用相同的策略, 例如,这是否会同时创建所有组件或创建基于在模板上声明的指令的组件。

1) i have 2 components such as app component, navbar component 2) app component have <app-navbar></app-navbar> on its template

据我了解 Angular 首先创建 root component 并将该组件视图粘贴到 index.html 上的 root component selector(app-root) 中,如果该根组件视图有另一个名为 app-navbar 的选择器,那么它只会创建实例navbar.component 并将该视图粘贴到 selector.correct 我如果我错了?

... and find the selector of this component in index.html and put the view of that root component

这仅发生在模块装饰器中定义为 bootstrap 组件的组件。因此这里:

Angular first create root component

最好使用术语 bootstrap 组件,因为可以有很多 bootstrap 个组件。

i have 2 components such as app component, navbar component

Angular 编译器会生成两个工厂。在此过程中,它将遇到 <app-navbar></app-navbar> 并创建适当的视图节点。当 Angular 将创建视图时,它将为 navbar component 创建一个视图并实例化 NavbarComponent class.

要了解有关 View 的更多信息,请从 Here is why you will not find components inside Angular

开始

更新

Angular只会实例化一个组件

  1. 遇到该组件的选择器
  2. 如果组件在entryComponent数组中提供
  3. 如果路由定义中包含组件