【问题标题】:Purpose of the bootstrap property in NgModuleNgModule 中 bootstrap 属性的用途
【发布时间】:2020-02-16 18:38:04
【问题描述】:

在 Angular 2+ 中,我了解了 NgModule,它可以通过配置对象传入。在这个对象内部,有一个“bootstrap”属性,它通常指向一个组件,比如 AppComponent。我想知道拥有这个“引导”属性有什么用?

加载 index.html 时,浏览器/角度引擎检测到自定义标签,它可以简单地搜索选择器为“app”的根模块的“声明”属性中定义的组件列表。一旦找到它,它就知道要引导的组件就是那个组件,比如 AppComponent。因此,为什么我们仍然需要手动定义这个“引导”属性。

【问题讨论】:

    标签: angular


    【解决方案1】:

    今天不小心碰到了https://angular.io/guide/entry-components的网站。我了解了 EntryComponent。

    An entry component is any component that Angular loads imperatively, (which means you’re not referencing it in the template), by type. You specify an entry component by bootstrapping it in an NgModule, or including it in a routing definition.
    

    为了回答我自己的问题,AppComponent 将被强制加载(由 Angular 框架,我认为)而不是在模板中引用它(index.html 不被视为模板),这就是为什么我们需要将它添加到bootstrap 属性,以便将其声明为入口组件。

    【讨论】:

    • 为什么我们需要在index.html 中声明<app-root></app-root>
    • 好问题。我想我们需要 以便它知道在 index.html 中呈现这个条目组件的位置。
    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    相关资源
    最近更新 更多