AppModule: 根模块

An Angular module class describes how the application parts fit together. Every application has at least one Angular module, the root module that you bootstrap to launch the application. You can call it anything you want. The conventional name is AppModule.

Angular 模块类描述应用的部件是如何组合在一起的。 每个应用都至少有一个 Angular 模块,也就是模块,用来引导并运行应用。 你可以为它取任何名字。常规名字是AppModule

The setup instructions produce a new project with the following minimal AppModule. You'll evolve this module as your application grows.

开发环境讲解了如何使用下面这个最小的AppModule来创建一个新项目。 这个模块随着应用的成长而演变。

src/app/app.module.ts

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }

After the import statements, you come to a class adorned with the @NgModule decorator.

import语句之后,可以看到一个@NgModule装饰器修饰的类。

The @NgModule decorator identifies AppModule as an Angular module class (also called an NgModule class). @NgModule takes a metadata object that tells Angular how to compile and launch the application.

@NgModule装饰器将AppModule标记为 Angular 模块类(也叫NgModule类)。 @NgModule接受一个元数据对象,告诉 Angular 如何编译和启动应用。

The Angular Modules (NgModules) guide dives deeply into the details of Angular modules. All you need to know at the moment is a few basics about these three properties.

Angular 模块 (NgModules) 指南深入讲解了 Angular 模块。 现在先初步了解这三个属性。

The imports array

imports 数组

Angular modules are a way to consolidate features that belong together into discrete units. Many features of Angular itself are organized as Angular modules. HTTP services are in the HttpModule. The router is in the RouterModule. Eventually you may create a feature module.

Angular 模块把特性合并成离散单元的一种方式。 Angular 自身的许多特性也是通过 Angular 模块组织的。 HTTP 服务在HttpModule里。路由器在RouterModule中。 最终,你可能也会创建特性模块。

Add a module to the imports array when the application requires its features.

当应用需要模块的特性时,将其添加到imports数组中。

This application, like most applications, executes in a browser. Every application that executes in a browser needs the BrowserModule from @angular/platform-browser. So every such application includes the BrowserModule in its root AppModule's imports array. Other guide and cookbook pages will tell you when you need to add additional modules to this array.

这个应用和大多数其他应用一样,在浏览器中运行。 每个浏览器中运行的应用都需要@angular/platform-browser里的BrowserModule。 所以每个这样的应用都在其AppModuleimports数组中包含BrowserModule。 在需要添加额外模块到此数组时,其他指南和烹饪宝典页面会告诉你。

Only NgModule classes go in the imports array. Don't put any other kind of class in imports.

imports数组中应该只有NgModule。不要放置其它类型的类。

Don't confuse the import statements at the top of the file with the Angular module's imports array. They have different jobs.

不要将 Angular 模块的imports数组与文件顶部的import语句弄混淆了。它们的功能不同。

The JavaScript import statements give you access to symbols exported by other files so you can reference them within this file. They have nothing to do with Angular and Angular knows nothing about them.

JavaScriptimport声明允许你访问在其他文件中导出的符号,这样你可以在当前文件引用它们。 它们与 Angular 毫无关系,Angular 对它们也一无所知。

The module's imports array tells Angular about specific Angular modules — classes decorated with @NgModule — that the application needs to function properly.

模块imports数组告诉 Angular 特定 Angular 模块的信息 — 用@NgModule装饰的类 — 应用需要它们来正常工作。

The declarations array

declarations 数组

You must declare every component in one (and only one) NgModule class. You tell Angular which components belong to the AppModule by listing it in the module's declarations array. As you create more components, you'll add them to declarations.

每个组件必须在且仅在一个NgModule类中声明。 通过将其列到AppModule模块的declarations数组中,告诉 Angular 哪个组件属于AppModule。 在创建更多组件的过程中,逐步将它们添加到declarations中。

You'll learn to create two other kinds of classes — directives and pipes — that you must also add to the declarations array.

你将会学习如何创建其他两种类 — 指令管道 — 它们也必须被添加到declarations数组。

Only declarablescomponents, directives and pipes — belong in the declarations array. Don't put any other kind of class in declarations; not NgModule classes, not service classes, not model classes.

只有*可以声明的组件指令管道 — 属于declarations数组。 不要将其他类型的类添加到declarations中,例如NgModule类, 服务类,模型类。

The bootstrap array

bootstrap 数组

You launch the application by bootstrapping the root AppModule. Among other things, the bootstrapping process creates the component(s) listed in the bootstrap array and inserts each one into the browser DOM.

通过引导AppModule来启动应用。 在启动过程中,其中一步是创建列在bootstrap数组的组件, 并将它们每一个都插入到浏览器的DOM中。

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

每个被引导的组件都是它自己的组件树的根。 插入一个被引导的组件通常触发一系列组件的创建并形成组件树。

While you can put more than one component tree on a host web page, that's not typical. Most applications have only one component tree and they bootstrap a single root component.

虽然你可以将多个组件树插入到宿主页面,但并不普遍。 大多数应用只有一个组件树,它们引导单一组件。

You can call the one root component anything you want but most developers call it AppComponent.

你可以为这个组件取任何名字,但是大多数程序员将其取名为AppComponent

Which brings us to the bootstrapping process itself.

下面让我们来看看引导过程本身。

Bootstrap in main.ts

main.ts中引导

There are many ways to bootstrap an application. The variations depend upon how you want to compile the application and where you want to run it.

引导应用的方法很多。 它们取决于你想如何编译应用以及应用将在哪儿运行。

In the beginning, you will compile the application dynamically with the Just-in-Time (JIT) compiler and you'll run it in a browser. You can learn about other options later.

开始时,你将使用即时 (JiT) 编译器动态编译应用。然后在浏览器中运行它。 稍后,你可以了解其他选项。

The recommended place to bootstrap a JIT-compiled browser application is in a separate file in the src folder named src/main.ts

引导即时编译的浏览器应用的推荐地点是在src目录中一个名为src/main.ts的单独文件中。

src/main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; platformBrowserDynamic().bootstrapModule(AppModule);

This code creates a browser platform for dynamic (JIT) compilation and bootstraps the AppModule described above.

上面的代码为动态 (JiT) 编译创建浏览器平台,并引导上面提到的AppModule

The bootstrapping process sets up the execution environment, digs the root AppComponent out of the module's bootstrap array, creates an instance of the component and inserts it within the element tag identified by the component's selector.

引导过程搭建运行环境,从模块的bootstrap数组中提出AppComponent, 创建该组件的实例,并将其插入到组件selector标识的元素标签中。

The AppComponent selector — here and in most documentation samples — is my-app so Angular looks for a <my-app> tag in the index.html like this one ...

AppComponent选择器 — 在这里以及文档大部分例子中 — 是my-app, 所以 Angular 在index.html中查找像这样的<my-app>标签...

<my-app><!-- content managed by Angular --></my-app>

... and displays the AppComponent there.

...然后在那儿显示AppComponent

This file is very stable. Once you've set it up, you may never change it again.

该文件非常稳定。一旦配置好,你可能永远不会再修改它。

QuickStart's AppModule

《快速起步》的AppModule

Every Angular application must have a root NgModule, even the QuickStart. You didn't see it but it was there.

每个 Angular 应用必须有一个根NgModule,包括《快速起步》在内。 你之前没有看到它,但它确定有。

A script in the index.html generated a hidden AppModule and bootstrapped it for you so you could focus on the AppComponent and discover the essential Angular more quickly.

index.html中的一个脚本生成了隐藏的AppModule并引导它。这样你可以专注于AppComponent,以更快的了解 Angular 的基础

If you're feeling adventurous, add your own AppModule and main.ts to the live code in the QuickStart plunker.

如果你爱冒险,可以在《快速起步》plunker 的在线代码中添加自己的AppModulemain.ts

Remove the following <script> tag from the index.html and see your work in action.

index.html中删除下面的<script>标签,看看的劳动成果。

Remove this script tag from "index.html"

<script> window.autoBootstrap = true; </script>

下一步

显示数据