angular ngModule 中的 entryComponents 是什么? [英] What is entryComponents in angular ngModule?

查看:32
本文介绍了angular ngModule 中的 entryComponents 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个依赖于 angular 2Ionic 应用程序 ( 2.0.0-rc0 ).所以包括了 ngModules 的新介绍.我在下面添加我的 app.module.ts..

I am working on an Ionic app ( 2.0.0-rc0 ) which depends on angular 2 . So the new introduction of ngModules is included. I am adding my app.module.ts. below.

import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { Users } from '../pages/users/users';

@NgModule({
  declarations: [
    MyApp,
    Users
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    Users
  ]
})
export class AppModule {}

entryComponents 在这里做什么?Components 已经在 declarations 中定义.那么有什么必要重复它们呢?如果我在此处不包含组件会怎样?

What does entryComponents do here? Components are already defined in declarations . So what's the need of repeating them ? What would happen if I dont include a component here?

推荐答案

这适用于使用 ViewContainerRef.createComponent() 添加的动态添加组件.将它们添加到 entryComponents 告诉离线模板编译器编译它们并为它们创建工厂.

This is for dynamically added components that are added using ViewContainerRef.createComponent(). Adding them to entryComponents tells the offline template compiler to compile them and create factories for them.

在路由配置中注册的组件也会自动添加到entryComponents 中,因为router-outlet 还使用了ViewContainerRef.createComponent() 来添加将组件路由到 DOM.

The components registered in route configurations are added automatically to entryComponents as well because router-outlet also uses ViewContainerRef.createComponent() to add routed components to the DOM.

离线模板编译器 (OTC) 仅构建实际使用的组件.如果不直接在模板中使用组件,OTC 无法知道它们是否需要编译.使用 entryComponents,您可以告诉 OTC 也编译这些组件,以便它们在运行时可用.

Offline template compiler (OTC) only builds components that are actually used. If components aren't used in templates directly the OTC can't know whether they need to be compiled. With entryComponents you can tell the OTC to also compile this components so they are available at runtime.

什么是入口组件?(angular.io)

NgModule 文档 (angular.io)

定义在定义此组件时也应编译的组件.对于此处列出的每个组件,Angular 将创建一个 ComponentFactory 并将其存储在 ComponentFactoryResolver 中.

Defines the components that should be compiled as well when this component is defined. For each components listed here, Angular will create a ComponentFactory and store it in the ComponentFactoryResolver.

如果您没有在 entryComponents 中列出动态添加的组件,您将收到一条关于缺少工厂的错误消息,因为 Angular 不会创建工厂.

If you don't list a dynamically added component to entryComponents you'll get an error message a bout a missing factory because Angular won't have created one.

另见 https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html

这篇关于angular ngModule 中的 entryComponents 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆