Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between declarations and entryComponents

Tags:

angular

I have this in my app.module.ts:

import { BrowserModule } from '@angular/platform-browser'; import { ErrorHandler, NgModule } from '@angular/core'; import { HttpModule, Http } from '@angular/http'; import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; import { EliteApi } from '../shared/shared'; import { MyApp } from './app.component'; import { MyTeams, Tournaments, TeamDetails, Teams, TeamHome, Standings } from '../pages/pages';  import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen';  @NgModule({     declarations: [         MyApp,         MyTeams,         TeamDetails,         Tournaments,         Teams,         TeamHome,         Standings     ],     imports: [         BrowserModule,         IonicModule.forRoot(MyApp),         HttpModule     ],     bootstrap: [IonicApp],     entryComponents: [         MyApp,         MyTeams,         TeamDetails,         Tournaments,         Teams,         TeamHome,         Standings     ],     providers: [         HttpModule,         StatusBar,         SplashScreen,         { provide: ErrorHandler, useClass: IonicErrorHandler },                 EliteApi     ] }) export class AppModule { } 

At the moment my declarations and entryComponents both are exactly the same. They contain all of the page/components that I built for my app. If I remove any entry from any of the properties I get error in angular2.

My question is if they are always the same then what is the need for these properties? I think I am definitely missing some point here. When would entryComponents and declaractions be different from one another?

like image 593
Tim Liberty Avatar asked May 06 '17 02:05

Tim Liberty


People also ask

What is entryComponents?

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.

What are the differences between declarations and providers in NgModule?

Declarations are used to make directives. Providers are used to make services. Imports makes the exported declarations of other modules available in the current module. Used to declare components, directives, pipes that belongs to the current module.

What is the use of entryComponents?

The entryComponents array is used to define only components that are not found in html and created dynamically. Angular requires this hint to find entry component and compile them.

What is the difference between exports and declarations in NgModule?

declarations: This property tells about the Components, Directives and Pipes that belong to this module. exports: The subset of declarations that should be visible and usable in the component templates of other NgModules.


1 Answers

The entryComponents array is used to define only components that are not found in html and created dynamically with ComponentFactoryResolver. Angular needs this hint to find them and compile. All other components should just be listed in the declarations array.

Here's the documentation on angular site

like image 127
Julia Passynkova Avatar answered Oct 16 '22 20:10

Julia Passynkova