2019-12-02 15:47:19 -05:00
|
|
|
|
# Frequently-used modules
|
2017-07-04 10:58:20 -04:00
|
|
|
|
|
2021-05-19 15:05:03 -04:00
|
|
|
|
An Angular application needs at least one module that serves as the root module.
|
2017-07-04 10:58:20 -04:00
|
|
|
|
As you add features to your app, you can add them in modules.
|
|
|
|
|
The following are frequently used Angular modules with examples
|
|
|
|
|
of some of the things they contain:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
|
|
|
|
|
<tr>
|
|
|
|
|
<th style="vertical-align: top">
|
|
|
|
|
NgModule
|
|
|
|
|
</th>
|
|
|
|
|
|
|
|
|
|
<th style="vertical-align: top">
|
|
|
|
|
Import it from
|
|
|
|
|
</th>
|
|
|
|
|
|
|
|
|
|
<th style="vertical-align: top">
|
|
|
|
|
Why you use it
|
|
|
|
|
</th>
|
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>BrowserModule</code></td>
|
|
|
|
|
<td><code>@angular/platform-browser</code></td>
|
2021-05-19 15:05:03 -04:00
|
|
|
|
<td>When you want to run your application in a browser</td>
|
2017-07-04 10:58:20 -04:00
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>CommonModule</code></td>
|
|
|
|
|
<td><code>@angular/common</code></td>
|
|
|
|
|
<td>When you want to use <code>NgIf</code>, <code>NgFor</code></td>
|
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>FormsModule</code></td>
|
|
|
|
|
<td><code>@angular/forms</code></td>
|
2018-06-02 04:18:11 -04:00
|
|
|
|
<td>When you want to build template driven forms (includes <code>NgModel</code>)</td>
|
2017-07-04 10:58:20 -04:00
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>ReactiveFormsModule</code></td>
|
|
|
|
|
<td><code>@angular/forms</code></td>
|
2018-06-02 04:18:11 -04:00
|
|
|
|
<td>When you want to build reactive forms</td>
|
2017-07-04 10:58:20 -04:00
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>RouterModule</code></td>
|
2018-01-17 07:57:43 -05:00
|
|
|
|
<td><code>@angular/router</code></td>
|
2018-06-02 04:18:11 -04:00
|
|
|
|
<td>When you want to use <code>RouterLink</code>, <code>.forRoot()</code>, and <code>.forChild()</code></td>
|
2017-07-04 10:58:20 -04:00
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
|
|
<tr>
|
2018-01-17 07:57:43 -05:00
|
|
|
|
<td><code>HttpClientModule</code></td>
|
|
|
|
|
<td><code>@angular/common/http</code></td>
|
2018-06-02 04:18:11 -04:00
|
|
|
|
<td>When you want to talk to a server</td>
|
2017-07-04 10:58:20 -04:00
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
## Importing modules
|
|
|
|
|
|
|
|
|
|
When you use these Angular modules, import them in `AppModule`,
|
|
|
|
|
or your feature module as appropriate, and list them in the `@NgModule`
|
2021-05-19 15:05:03 -04:00
|
|
|
|
`imports` array. For example, in the basic application generated by the [Angular CLI](cli),
|
2017-07-04 10:58:20 -04:00
|
|
|
|
`BrowserModule` is the first import at the top of the `AppModule`,
|
2018-01-17 07:57:43 -05:00
|
|
|
|
`app.module.ts`.
|
2017-07-04 10:58:20 -04:00
|
|
|
|
|
|
|
|
|
|
2018-01-17 07:57:43 -05:00
|
|
|
|
```typescript
|
2017-07-04 10:58:20 -04:00
|
|
|
|
/* import modules so that AppModule can access them */
|
|
|
|
|
import { BrowserModule } from '@angular/platform-browser';
|
|
|
|
|
import { NgModule } from '@angular/core';
|
|
|
|
|
|
|
|
|
|
import { AppComponent } from './app.component';
|
|
|
|
|
|
|
|
|
|
@NgModule({
|
2018-01-17 07:57:43 -05:00
|
|
|
|
declarations: [
|
|
|
|
|
AppComponent
|
|
|
|
|
],
|
|
|
|
|
imports: [ /* add modules here so Angular knows to use them */
|
|
|
|
|
BrowserModule,
|
|
|
|
|
],
|
|
|
|
|
providers: [],
|
|
|
|
|
bootstrap: [AppComponent]
|
2017-07-04 10:58:20 -04:00
|
|
|
|
})
|
|
|
|
|
export class AppModule { }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The imports at the top of the array are JavaScript import statements
|
|
|
|
|
while the `imports` array within `@NgModule` is Angular specific.
|
|
|
|
|
For more information on the difference, see [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## `BrowserModule` and `CommonModule`
|
|
|
|
|
|
|
|
|
|
`BrowserModule` imports `CommonModule`, which contributes many common
|
|
|
|
|
directives such as `ngIf` and `ngFor`. Additionally, `BrowserModule`
|
|
|
|
|
re-exports `CommonModule` making all of its directives available
|
|
|
|
|
to any module that imports `BrowserModule`.
|
|
|
|
|
|
2021-05-19 15:05:03 -04:00
|
|
|
|
For applications that run in the browser, import `BrowserModule` in the
|
2017-07-04 10:58:20 -04:00
|
|
|
|
root `AppModule` because it provides services that are essential
|
2021-05-19 15:05:03 -04:00
|
|
|
|
to launch and run a browser application. `BrowserModule`’s providers
|
|
|
|
|
are for the whole application so it should only be in the root module,
|
2017-07-04 10:58:20 -04:00
|
|
|
|
not in feature modules. Feature modules only need the common
|
|
|
|
|
directives in `CommonModule`; they don’t need to re-install app-wide providers.
|
|
|
|
|
|
|
|
|
|
If you do import `BrowserModule` into a lazy loaded feature module,
|
|
|
|
|
Angular returns an error telling you to use `CommonModule` instead.
|
|
|
|
|
|
2019-11-11 17:47:51 -05:00
|
|
|
|
<div class="lightbox">
|
|
|
|
|
<img src="generated/images/guide/frequent-ngmodules/browser-module-error.gif" width=750 alt="BrowserModule error">
|
|
|
|
|
</div>
|
2017-07-04 10:58:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## More on NgModules
|
|
|
|
|
|
|
|
|
|
You may also be interested in the following:
|
|
|
|
|
* [Bootstrapping](guide/bootstrapping).
|
|
|
|
|
* [NgModules](guide/ngmodules).
|
|
|
|
|
* [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule).
|