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.
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.
imports — BrowserModule,这个和每个在浏览器中运行应用都需要它。
imports — the BrowserModule that this and every application needs to run in a browser.
declarations — 应用的唯一组件,它同时也是...
declarations — the application's lone component, which is also ...
bootstrap — 根组件,Angular 创建它并插入index.html宿主页面。
bootstrap — the root component that Angular creates and inserts into the index.html host web page.
The Angular Modules (NgModule) 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 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.
当应用需要模块的特性时,将其添加到imports数组中。
Add a module to the imports array when the application requires its features.
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 rootAppModule's imports array.
Other guide and cookbook pages will tell you when you need to add additional modules to this array.
imports数组中应该只有NgModule类。不要放置其它类型的类。
Only NgModule classes go in the imports array. Do not put any other kind of class in imports.
The JavaScriptimport statements give you access to symbols exported by other files
so you can reference them within this file.
You add import statements to almost every application file.
They have nothing to do with Angular and Angular knows nothing about them.
The module'simports array appears exclusively in the @NgModule metadata object.
It tells Angular about specific other Angular modules — all of them classes decorated with @NgModule —
that the application needs to function properly.
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.
You must declare every component in an NgModule class.
If you use a component without declaring it, you'll see a clear error message in the browser console.
Only declarables — components, directives and pipes — belong in the declarations array.
Do not put any other kind of class in declarations; notNgModule classes, not service classes, not model classes.
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.
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.
你可以为这个根组件取任何名字,但是大多数程序员将其取名为AppComponent。
You can call the one root component anything you want but most developers call it AppComponent.
下面让我们来看看引导过程本身。
Which brings us to the bootstrapping process itself.
在main.ts中引导
Bootstrap in 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.
引导即时编译的浏览器应用的推荐地点是在src目录中一个名为src/main.ts的单独文件中。
The recommended place to bootstrap a JIT-compiled browser application is in a separate file
in the src folder named src/main.ts
The bootstrapping process sets up the execution environment,
digs the rootAppComponent 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.
Your initial app has only a single module, the root module.
As your app grows, you'll consider subdividing it into multiple "feature" modules,
some of which can be loaded later ("lazy loaded") if and when the user chooses
to visit those features.