angular ngModule中的 entryComponents是什么

angular ngModule中的 entryComponents是什么

entryComponent是强制加载angular的组件,这意味着这些组件在HTML模板中没有被引用。在大多数情况下,Angular会在组件模板中明确声明的情况下加载一个组件。但对于entryComponents来说,情况并非如此。entryComponents只被动态加载,在组件模板中从不被引用。它指的是在HTML中找不到的组件阵列,而是由ComponentFactoryResolver添加的。

首先,Angular通过ComponentFactoryResolver类为每个bootstrap entryComponents创建一个组件工厂,然后在运行时,它将使用工厂来实例化这些组件。

abstract class ComponentFactoryResolver {
  static NULL: ComponentFactoryResolver
  abstract resolveComponentFactory(component: Type): ComponentFactory
}
JavaScript

Angular中的入口组件的类型:

  1. 被引导的根部组件
  2. 路由组件(你在路由中指定的组件)。

Bootstrapped entryComponent:在应用程序启动时或在引导过程中,引导组件被Angular加载到DOM(文档对象模型)。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
  
import { AppComponent } from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
JavaScript

bootstrap组件是一个EntryComponent,它提供了应用程序的入口点。Angular默认加载AppComponent,因为它被列在@ NgModule.bootstrap.

什么是angular ngModule中的 entryComponents?

app.module.ts

Routed entryComponent:这种组件是声明的组件,在应用程序的声明部分内作为数组添加。但是,你可能需要通过它的类来引用一个组件。路由器组件在组件的HTML中没有明确指定,但是,在路由数组中注册。这些组件也是动态加载的,因此Angular需要了解它们。
这些组件是在两个地方添加的。

  • Router
  • entryComponents

app-routing.module.ts

import { NgModule } from '@angular/core';
import {  RouterModule, Routes } from '@angular/router';
import { ListComponent } from './list/list.component';
import { DetailComponent } from './detail/detail.component';
  
const routes: Routes = [
  {
     path:'',
     redirectTo:'/contact', 
     pathMatch:'full'
  },
  {
    path: 'list',
    component: ListComponent
  },
  {
    path: 'detail',
    component: DetailComponent
  },
  { 
    path:'**',
    redirectTo:'/not-found'
  }
];
@NgModule({
    imports:[RouterModule.forRoot(routes)],
    exports:[RouterModule]
  
})
export class AppRoutingModule{
  
}
JavaScript

你不需要明确地将组件添加到entryComponent数组中,Angular会自动这样做。编译器会将所有的路由器组件添加到entryComponent数组中。
什么是angular ngModule中的 entryComponents?

entryComponent数组的需要: Angular只在最终包中包括那些在模板中被引用的组件。这样做是为了通过不包括不需要的组件来减少最终包的大小。但这将破坏最终的应用程序,因为所有的entryComponents都不会被包含在最终的包中(因为它们从未被引用过)。因此,我们需要在entyComponents数组下注册这些组件,让Angular将它们包含在包中。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册