{ "id": "guide/bootstrapping", "title": "Launching your app with a root module", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Launching your app with a root modulelink

\n

Prerequisiteslink

\n

A basic understanding of the following:

\n\n
\n

An NgModule describes how the application parts fit together.\nEvery application has at least one Angular module, the root module,\nwhich must be present for bootstrapping the application on launch.\nBy convention and by default, this NgModule is named AppModule.

\n

When you use the Angular CLI command ng new to generate an app, the default AppModule looks like the following:

\n\n/* JavaScript imports */\nimport { BrowserModule } from '@angular/platform-browser';\nimport { NgModule } from '@angular/core';\n\nimport { AppComponent } from './app.component';\n\n/* the AppModule class with the @NgModule decorator */\n@NgModule({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule\n ],\n providers: [],\n bootstrap: [AppComponent]\n})\nexport class AppModule { }\n\n

After the import statements is a class with the\n@NgModule decorator.

\n

The @NgModule decorator identifies AppModule as an NgModule class.\n@NgModule takes a metadata object that tells Angular how to compile and launch the application.

\n\n

The default application created by the Angular CLI only has one component, AppComponent, so it\nis in both the declarations and the bootstrap arrays.

\n\n

The declarations arraylink

\n

The module's declarations array tells Angular which components belong to that module.\nAs you create more components, add them to declarations.

\n

You must declare every component in exactly one NgModule class.\nIf you use a component without declaring it, Angular returns an\nerror message.

\n

The declarations array only takes declarables. Declarables\nare components, directives and pipes.\nAll of a module's declarables must be in the declarations array.\nDeclarables must belong to exactly one module. The compiler emits\nan error if you try to declare the same class in more than one module.

\n

These declared classes are visible within the module but invisible\nto components in a different module unless they are exported from\nthis module and the other module imports this one.

\n

An example of what goes into a declarations array follows:

\n\n declarations: [\n YourComponent,\n YourPipe,\n YourDirective\n ],\n\n

A declarable can only belong to one module, so only declare it in\none @NgModule. When you need it elsewhere,\nimport the module that has the declarable you need in it.

\n

Using directives with @NgModulelink

\n

Use the declarations array for directives.\nTo use a directive, component, or pipe in a module, you must do a few things:

\n
    \n
  1. Export it from the file where you wrote it.
  2. \n
  3. Import it into the appropriate module.
  4. \n
  5. Declare it in the @NgModule declarations array.
  6. \n
\n

Those three steps look like the following. In the file where you create your directive, export it.\nThe following example, named ItemDirective is the default directive structure that the CLI generates in its own file, item.directive.ts:

\n\n\n\nimport { Directive } from '@angular/core';\n\n@Directive({\n selector: '[appItem]'\n})\nexport class ItemDirective {\n// code goes here\n constructor() { }\n\n}\n\n\n

The key point here is that you have to export it so you can import it elsewhere. Next, import it\ninto the NgModule, in this example app.module.ts, with a JavaScript import statement:

\n\nimport { ItemDirective } from './item.directive';\n\n\n

And in the same file, add it to the @NgModule declarations array:

\n\ndeclarations: [\n AppComponent,\n ItemDirective\n],\n\n\n

Now you could use your ItemDirective in a component. This example uses AppModule, but you'd do it the same way for a feature module. For more about directives, see Attribute Directives and Structural Directives. You'd also use the same technique for pipes and components.

\n

Remember, components, directives, and pipes belong to one module only. You only need to declare them once in your app because you share them by importing the necessary modules. This saves you time and helps keep your app lean.

\n\n

The imports arraylink

\n

The module's imports array appears exclusively in the @NgModule metadata object.\nIt tells Angular about other NgModules that this particular module needs to function properly.

\n\nimports: [\n BrowserModule,\n FormsModule,\n HttpClientModule\n],\n\n\n

This list of modules are those that export components, directives, or pipes\nthat component templates in this module reference. In this case, the component is\nAppComponent, which references components, directives, or pipes in BrowserModule,\nFormsModule, or HttpClientModule.\nA component template can reference another component, directive,\nor pipe when the referenced class is declared in this module or\nthe class was imported from another module.

\n\n

The providers arraylink

\n

The providers array is where you list the services the app needs. When\nyou list services here, they are available app-wide. You can scope\nthem when using feature modules and lazy loading. For more information, see\nProviders.

\n

The bootstrap arraylink

\n

The application launches by bootstrapping the root AppModule, which is\nalso referred to as an entryComponent.\nAmong other things, the bootstrapping process creates the component(s) listed in the bootstrap array\nand inserts each one into the browser DOM.

\n

Each bootstrapped component is the base of its own tree of components.\nInserting a bootstrapped component usually triggers a cascade of\ncomponent creations that fill out that tree.

\n

While you can put more than one component tree on a host web page,\nmost applications have only one component tree and bootstrap a single root component.

\n

This one root component is usually called AppComponent and is in the\nroot module's bootstrap array.

\n

More about Angular Moduleslink

\n

For more on NgModules you're likely to see frequently in apps,\nsee Frequently Used Modules.

\n\n \n
\n\n\n" }