问题描述
我正在使用 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.
如果没有列出动态添加的组件到 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.
另见
这篇关于什么是angular ngModule中的entryComponents?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!