angular-cn/aio/content/guide/bootstrapping.md

11 KiB
Raw Blame History

Bootstrapping

启动过程

An NgModule class describes how the application parts fit together. Every application has at least one NgModule, 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来创建一个新项目。 这个模块随着应用的成长而演变。

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

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

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

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

  • imports — the BrowserModule that this and every application needs to run in a browser.

    importsBrowserModule,这个和每个在浏览器中运行应用都需要它。

  • declarations — the application's lone component, which is also ...

    declarations — 应用的唯一组件,它同时也是...

  • bootstrap — the root component that Angular creates and inserts into the index.html host web page.

    bootstrap — _根_组件Angular 创建它并插入index.html宿主页面。

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

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

{@a imports}

The imports array

imports数组

NgModules are a way to consolidate features that belong together into discrete units. Many features of Angular itself are organized as NgModules. 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. Do not put any other kind of class in imports.

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

The import statements at the top of the file and the NgModule's imports array are unrelated and have completely 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. You add import statements to almost every application file. They have nothing to do with Angular and Angular knows nothing about them.

JavaScriptimport声明允许你访问在其他文件中_导出_的符号这样你可以在_当前_文件引用它们。 我们会往几乎所有类型的应用中加入import语句。 它们与 Angular 毫无关系Angular 对它们也一无所知。

The module's imports array appears exclusively in the @NgModule metadata object. It tells Angular about specific other NgModules—all of them classes decorated with @NgModule—that the application needs to function properly.

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

{@a declarations}

The declarations array

declarations 数组

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.

通过将其列到AppModule模块的declarations数组中,可以告诉 Angular 哪个组件属于AppModule。 在创建更多组件的过程中,逐步将它们添加到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.

你必须在一个NgModule类声明每一个组件。 否则当你使用这些组件时就会在浏览器的控制台中看到一个明显的错误信息。

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. Do not put any other kind of class in declarations; not NgModule classes, not service classes, not model classes.

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

{@a bootstrap-array}

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.

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

{@a main}

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的单独文件中。

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>标签...

... and displays the AppComponent there.

...然后在那儿显示AppComponent

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

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

More about NgModules

关于Angular模块的更多知识

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.

你最初的应用只有一个单一的模块 —— 模块。 当这个应用不断成长时,你就要考虑把它们拆分到多个 "特性" 模块中了。 这些特性模块中的一部分可以稍后加载(即惰性加载),它们只会在用户访问到这些特性时才会加载。

When you're ready to explore these possibilities, visit the NgModules guide.

如果你要了解这些知识,请访问Angular 模块 (NgModule)