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

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

问题描述

我正在使用 Ionic 应用程序( 2.0.0-rc0 ),这取决于角度2 。因此包含了新的 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 {}

<$ c $是什么c> entryComponents 在这做什么? 组件已在声明中定义。那么重复它们的需要是什么?如果我在这里不包含组件会发生什么?

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?

推荐答案

这是用于使用<$ c $添加的动态添加组件C> 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 docs(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天全站免登陆