diff --git a/aio/tools/translator/bin/translate-one.ts b/aio/tools/translator/bin/translate-one.ts index ee6b2252d4..c415acbfbd 100644 --- a/aio/tools/translator/bin/translate-one.ts +++ b/aio/tools/translator/bin/translate-one.ts @@ -3,5 +3,5 @@ import { dirs } from '../dirs'; import { translateFile } from '../translate'; -const filename = 'guide/router.md'; +const filename = 'guide/testing.md'; translateFile(__dirname + '/../../../../../content-en/' + filename, dirs.content + filename); diff --git a/aio/tools/translator/dict-latest.json b/aio/tools/translator/dict-latest.json index 14b4e32f0e..aeed9b9b2e 100644 --- a/aio/tools/translator/dict-latest.json +++ b/aio/tools/translator/dict-latest.json @@ -864,6 +864,11 @@ "translation": "Angular动画是基于标准的[Web动画API(Web Animations API)](https://w3c.github.io/web-animations/)构建的,它们在[支持此API的浏览器中](http://caniuse.com/#feat=web-animation)会用原生方式工作。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/animations.md" }, + { + "original": "As of Angular 6, If the Web Animations API is not supported natively by the browser, then Angular will use CSS\nkeyframes as a fallback instead (automatically). This means that the polyfill is no longer required unless any\ncode uses [AnimationBuilder](/api/animations/AnimationBuilder). If your code does use AnimationBuilder, then\nuncomment the `web-animations-js` polyfill from the `polyfills.ts` file generated by Angular CLI.", + "translation": "对于 Angular 6,如果浏览器没有提供对 Web 动画 API 的原生支持,Angular 就会自动改用 CSS 的关键帧动画作为后备实现。这意味,除非要在代码中使用 [AnimationBuilder](/api/animations/AnimationBuilder) ,否则不必使用相关的腻子脚本。\n如果你要在代码中使用 AnimationBuilder ,就要从 Angular CLI 自动生成的 `polyfills.ts` 文件中反注释掉 `web-animations-js` 腻子脚本。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/animations.md" + }, { "original": "The examples in this page are available as a .", "translation": "本章中引用的这个例子可以到去体验。", @@ -874,6 +879,31 @@ "translation": "## 准备工作", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/animations.md" }, + { + "original": "Before you can add animations to your application, you need\nto import a few animation-specific modules and functions to the root application module.", + "translation": "在往应用中添加动画之前,你要首先在应用的根模块中引入一些与动画有关的模块和函数。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/animations.md" + }, + { + "original": "#### Example basics", + "translation": "#### 基本例子", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/animations.md" + }, + { + "original": "The animations examples in this guide animate a list of heroes.", + "translation": "这里的动画例子用来给英雄列表添加动画。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/animations.md" + }, + { + "original": "A `Hero` class has a `name` property, a `state` property that indicates if the hero is active or not,\nand a `toggleState()` method to switch between the states.", + "translation": "`Hero` 类有一个 `name` 属性、一个 `state` 属性(用于表明该英雄是否为激活状态)和一个 `toggleState()` 函数,用来在这两种状态之间切换。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/animations.md" + }, + { + "original": "Across the top of the screen (`app.hero-team-builder.component.ts`)\nare a series of buttons that add and remove heroes from the list (via the `HeroService`). \nThe buttons trigger changes to the list that all of the example components see at the same time.", + "translation": "在屏幕的顶部(`app.hero-team-builder.component.ts`)是一系列按钮,用于从列表中添加和删除英雄(通过 `HeroService`)。\n这些按钮会引起列表的变化,同时可以看到列表中的所有范例组件。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/animations.md" + }, { "original": "## Transitioning between two states", "translation": "## 快速起步范例:在两个状态间转场", @@ -2301,7 +2331,7 @@ }, { "original": "Try the .", - "translation": "你可以到这里试试:。", + "translation": "你可以到这里试试:。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/attribute-directives.md" }, { @@ -2439,6 +2469,16 @@ "translation": "我们可以在指令的构造函数中注入`ElementRef`,来引用宿主 DOM 元素,", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/attribute-directives.md" }, + { + "original": "`ElementRef` grants direct access to the host DOM element\nthrough its `nativeElement` property.", + "translation": "`ElementRef` 通过其 `nativeElement` 属性给你了直接访问宿主 DOM 元素的能力。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/attribute-directives.md" + }, + { + "original": "This first implementation sets the background color of the host element to yellow.", + "translation": "这里的第一个实现把宿主元素的背景色设置为了黄色。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/attribute-directives.md" + }, { "original": "## Apply the attribute directive", "translation": "## 使用属性型指令", @@ -2504,9 +2544,14 @@ "translation": "必须直接和 DOM API 打交道,应该避免这样做。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/attribute-directives.md" }, + { + "original": "The handlers delegate to a helper method that sets the color on the host DOM element, `el`.", + "translation": "这些处理器委托了一个辅助方法来为DOM元素(`el`)设置颜色。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/attribute-directives.md" + }, { "original": "The helper method, `highlight`, was extracted from the constructor.\nThe revised constructor simply declares the injected `el: ElementRef`.", - "translation": "这些处理器委托给了一个辅助方法,它用于为DOM元素设置颜色,`el`就是你在构造器中声明和初始化过的。", + "translation": "这个辅助方法(`highlight`)被从构造函数中提取了出来。\n修改后的构造函数只负责声明要注入的元素 `el: ElementRef`。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/attribute-directives.md" }, { @@ -2521,7 +2566,7 @@ }, { "original": "## Pass values into the directive with an _@Input_ data binding", - "translation": "## 使用数据绑定向指令传递值", + "translation": "## 使用 `@Input` 数据绑定向指令传递值", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/attribute-directives.md" }, { @@ -2804,14 +2849,194 @@ "translation": "# 启动过程", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" }, + { + "original": "#### Prerequisites", + "translation": "#### 前提条件", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "A basic understanding of the following:", + "translation": "对下列知识有基本的了解:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "* [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule).", + "translation": "[JavaScript 模块与 NgModules](guide/ngmodule-vs-jsmodule).", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "An NgModule describes how the application parts fit together.\nEvery application has at least one Angular module, the _root_ module\nthat you bootstrap to launch the application.\nBy convention, it is usually called `AppModule`.", + "translation": "NgModule 用于描述应用的各个部分如何组织在一起。\n每个应用又至少一个 Angular 模块,*根*模块就是你用来启动此应用的模块。\n按照惯例,它通常命名为 `AppModule`。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "If you use the CLI to generate an app, the default `AppModule` is as follows:", + "translation": "如果你使用 CLI 来生成一个应用,其默认的 `AppModule` 是这样的:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "After the import statements is a class with the\n**`@NgModule`** [decorator](guide/glossary#decorator '\"Decorator\" explained').", + "translation": "在 `import` 语句之后,是一个带有 **`@NgModule`** [装饰器](guide/glossary#decorator '\"Decorator\" explained')的类。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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.", + "translation": "`@NgModule` 装饰器表明 `AppModule` 是一个 `NgModule` 类。\n`@NgModule` 获取一个元数据对象,它会告诉 Angular 如何编译和启动本应用。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "* **_declarations_**—this application's lone component.", + "translation": "**_declarations_** —— 该应用所拥有的组件。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "* **_imports_**—import `BrowserModule` to have browser specific services such as DOM rendering, sanitization, and location.", + "translation": "**_imports_** —— 导入 `BrowserModule` 以获取浏览器特有的服务,比如 DOM 渲染、无害化处理和位置(location)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "* **_providers_**—the service providers.", + "translation": "**_providers_** —— 各种服务提供商。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, { "original": "* **_bootstrap_**—the _root_ component that Angular creates and inserts\ninto the `index.html` host web page.", - "translation": "**_bootstrap_** — _根_组件,Angular 创建它并插入`index.html`宿主页面。", + "translation": "**_bootstrap_** —— *根*组件,Angular 创建它并插入`index.html`宿主页面。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "The default CLI application only has one component, `AppComponent`, so it\nis in both the `declarations` and the `bootstrap` arrays.", + "translation": "默认的 CLI 应用只有一个组件 `AppComponent`,所以它会同时出现在 `declarations` 和 `bootstrap` 数组中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "## The `declarations` array", + "translation": "## `declarations` 数组", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "The module's `declarations` array tells Angular which components belong to that module.\nAs you create more components, add them to `declarations`.", + "translation": "该模块的 `declarations` 数组告诉 Angular 哪些组件属于该模块。\n当你创建更多组件时,也要把它们添加到 `declarations` 中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "You must declare every component in exactly one `NgModule` class.\nIf you use a component without declaring it, Angular returns an\nerror message.", + "translation": "每个组件都应该(且只能)声明(declare)在一个 `NgModule` 类中。 如果你使用了未声明过的组件,Angular 就会报错。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "The `declarations` array only takes declarables. Declarables\nare components, [directives](guide/attribute-directives) and [pipes](guide/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.", + "translation": "`declarations` 数组只能接受可声明对象。可声明对象包括组件、[指令](guide/attribute-directives)和[管道](guide/pipes)。\n一个模块的所有可声明对象都必须放在 `declarations` 数组中。\n可声明对象必须只能属于一个模块,如果同一个类被声明在了多个模块中,编译器就会报错。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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.", + "translation": "这些可声明的类在当前模块中是可见的,但是对其它模块中的组件是不可见的 —— 除非把它们从当前模块导出,\n并让对方模块导入本模块。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "An example of what goes into a declarations array follows:", + "translation": "下面是哪些类可以添加到 `declarations` 数组中的例子:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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.", + "translation": "每个可声明对象都只能属于一个模块,所以只能把它声明在一个 `@NgModule` 中。当你需要在其它模块中使用它时,就要在那里导入包含这个可声明对象的模块。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "**Only `@NgModule` references** go in the `imports` array.", + "translation": "**只有 `@NgModule`** 可以出现在 `imports` 数组中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "### Using directives with `@NgModule`", + "translation": "### 通过 `@NgModule` 使用指令", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "Use the `declarations` array for directives.\nTo use a directive, component, or pipe in a module, you must do a few things:", + "translation": "使用 `declarations` 数组声明指令。在模块中使用指令、组件或管道的步骤如下:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "1. Export it from the file where you wrote it.", + "translation": "从你编写它的文件中导出它。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "2. Import it into the appropriate module.", + "translation": "把它导入到适当的模块中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "3. Declare it in the `@NgModule` `declarations` array.", + "translation": "在 `@NgModule` 的 `declarations` 数组中声明它。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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`:", + "translation": "这三步的结果如下所示。在你创建指令的文件中导出它。\n下面的例子中,`item.directive.ts` 中的 `ItemDirective` 是 CLI 自动生成的默认指令结构。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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:", + "translation": "重点在于你要先在这里导出它才能在别处导入它。接下来,使用 JavaScript 的 `import` 语句把它导入到 NgModule 中(这里是 `app.module.ts`)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "And in the same file, add it to the `@NgModule` `declarations` array:", + "translation": "同样在这个文件中,把它添加到 `@NgModule` 的 `declarations` 数组中:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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](guide/attribute-directives) and [Structural Directives](guide/structural-directives). You'd also use the same technique for [pipes](guide/pipes) and components.", + "translation": "现在,你就可以在组件中使用 `ItemDirective` 了。这个例子中使用的是 `AppModule`,但是在特性模块中你也可以这么做。\n要进一步了解指令,参见[属性型指令](guide/attribute-directives)和[结构型指令](guide/structural-directives)。\n这些也同样适用于[管道](guide/pipes)和组件。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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.", + "translation": "记住:组件、指令和管道都只能属于一个模块。你在应用中也只需要声明它们一次,因为你还可以通过导入必要的模块来使用它们。这能节省你的时间,并且帮助你的应用保持精简。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "## The `imports` array", + "translation": "## `imports` 数组", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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.", + "translation": "模块的 `imports` 数组只会出现在 `@NgModule` 元数据对象中。\n它告诉 Angular 该模块想要正常工作,还需要哪些模块。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "This list of modules are those that export components, directives, or pipes\nthat the component templates in this module reference. In this case, the component is\n`AppComponent`, which references components, directives, or pipes in `BrowserModule`,\n`FormsModule`, or `HttpModule`.\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.", + "translation": "列表中的模块导出了本模块中的各个组件模板中所引用的各个组件、指令或管道。在这个例子中,当前组件是 `AppComponent`,它引用了导出自 `BrowserModule`、`FormsModule` 或 `HttpModule` 的组件、指令或管道。\n总之,组件的模板中可以引用在当前模块中声明的或从其它模块中导入的组件、指令、管道。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" }, { "original": "You don't have any services to provide yet.\nBut you will create some before long and you may chose to provide many of them here.", - "translation": "我们还没有提供任何服务,但是很快就会创建一些,而且可能也会选择在这里提供它们。", + "translation": "你还没有提供任何服务,但是很快就会创建一些,而且可能也会选择在这里提供它们。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "## The `providers` array", + "translation": "## `providers` 数组", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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\n[Providers](guide/providers).", + "translation": "`providers` 数组中列出了该应用所需的服务。当直接把服务列在这里时,它们是全应用范围的。\n当你使用特性模块和惰性加载时,它们是范围化的。要了解更多,参见[服务提供商](guide/providers)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "## The `bootstrap` array", + "translation": "## `bootstrap` 数组", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "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.", + "translation": "应用是通过引导根模块 `AppModule` 来启动的,根模块还引用了 `entryComponent`。\n此外,引导过程还会创建 `bootstrap` 数组中列出的组件,并把它们逐个插入到浏览器的 DOM 中。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" }, { @@ -2819,11 +3044,26 @@ "translation": "每个被引导的组件都是它自己的组件树的根。\n插入一个被引导的组件通常触发一系列组件的创建并形成组件树。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" }, + { + "original": "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.", + "translation": "虽然也可以在宿主页面中放多个组件,但是大多数应用只有一个组件树,并且只从一个根组件开始引导。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, + { + "original": "This one root component is usually called `AppComponent` and is in the\nroot module's `bootstrap` array.", + "translation": "这个根组件通常叫做 `AppComponent`,并且位于根模块的 `bootstrap` 数组中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, { "original": "## More about Angular Modules", "translation": "## 关于Angular模块的更多知识", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" }, + { + "original": "For more on NgModules you're likely to see frequently in apps,\nsee [Frequently Used Modules](#).", + "translation": "要进一步了解常见的 NgModules 知识,参见 [关于模块的常见问题](#)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/bootstrapping.md" + }, { "original": "# Browser support", "translation": "# 浏览器支持", @@ -3514,6 +3754,11 @@ "translation": "

速查表

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Bootstrapping", + "translation": "引导/启动", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Bootstraps the app, using the root component from the specified NgModule.

", "translation": "

NgModule 中指定的根组件进行启动。

", @@ -3599,6 +3844,11 @@ "translation": "

创建一个局部变量movieplayer,支持在当前模板的数据绑定和事件绑定表达式中访问video元素的实例。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "

The * symbol turns the current element into an embedded template. Equivalent to:\n<ng-template [myUnless]=\"myExpression\"><p>...</p></ng-template>

", + "translation": "

这个 * 符号会把当前元素转换成一个内嵌的模板。它等价于:\n<ng-template [myUnless]=\"myExpression\"><p>...</p></ng-template>

", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Transforms the current value of expression cardNumber via the pipe called myCardNumberFormatter.

", "translation": "

使用名叫myCardNumberFormatter的管道对表达式cardNumber的当前值进行变幻

", @@ -3619,6 +3869,11 @@ "translation": "

<svg>作为根元素时会自动识别为 SVG 元素,不需要前缀。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Built-in directives", + "translation": "内置指令", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Removes or recreates a portion of the DOM tree based on the showSection expression.

", "translation": "

根据showSection表达式的结果,移除或重新创建 DOM 树的一部分。

", @@ -3639,11 +3894,26 @@ "translation": "

根据 map 中的 value 是否为真,来决定该元素上是否出现与 name 对应的 CSS 类。右侧的表达式应该返回一个形如 {class-name: true/false} 的 map。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "

Allows you to assign styles to an HTML element using CSS. You can use CSS directly, as in the first example, or you can call a method from the component.

", + "translation": "

允许你使用 CSS 为 HTML 元素指定样式。你可以像第一个例子那样直接使用 CSS,也可以调用组件中的方法。

", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, + { + "original": "Forms", + "translation": "表单", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Provides two-way data-binding, parsing, and validation for form controls.

", "translation": "

为表单控件提供双向数据绑定、解析和验证功能。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Class decorators", + "translation": "类装饰器", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Declares that a class is a component and provides metadata about the component.

", "translation": "

声明一个类是组件,并提供该组件的元数据。

", @@ -3659,6 +3929,11 @@ "translation": "

声明一个类是管道,并提供该管道的元数据。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Directive configuration", + "translation": "指令配置项", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Specifies a CSS selector that identifies this directive within a template. Supported selectors include element,\n[attribute], .class, and :not().

", "translation": "

指定一个 CSS 选择器,用于在模板中标记出该指令。支持的选择器类型包括:元素名[属性名], .类名:not()

", @@ -3674,6 +3949,16 @@ "translation": "

该指令及其子指令的依赖注入提供商列表。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Component configuration", + "translation": "组件配置项", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, + { + "original": "

@Component extends @Directive,\nso the @Directive configuration applies to components as well

", + "translation": "

@Component 继承自 @Directive,\n因此,@Directive 的这些配置项也同样适用于组件。

", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

If set, the templateUrl and styleUrl are resolved relative to the component.

", "translation": "

如果设置了,那么 templateUrlstyleUrl 的路径就会相对于当前组件进行解析。

", @@ -3694,6 +3979,11 @@ "translation": "

用于为当前组件的视图提供样式的内联 CSS 或外部样式表 URL 的列表。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Class field decorators for directives and components", + "translation": "给指令和组件使用的类属性配置项", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Declares an input property that you can update via property binding (example:\n<my-cmp [myProperty]=\"someExpression\">).

", "translation": "

声明一个输入属性,你可以通过属性绑定来更新它,如 <my-cmp [myProperty]=\"someExpression\">

", @@ -3734,6 +4024,11 @@ "translation": "

把组件视图查询(myPredicate)的全部结果绑定到该类的 myChildComponents 属性上。对指令无效。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Directive and component change detection and lifecycle hooks", + "translation": "指令与组件的变更检测与生命周期钩子", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Called before any other lifecycle hook. Use it to inject dependencies, but avoid any serious work here.

", "translation": "

在任何其它生命周期钩子之前调用。可以用它来注入依赖项,但不要在这里做正事。

", @@ -3779,6 +4074,11 @@ "translation": "

只在实例被销毁前调用一次。

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Dependency injection configuration", + "translation": "依赖注入配置项", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Sets or overrides the provider for MyService to the MyMockService class.

", "translation": "

MyService 的服务提供商设置或改写为 MyMockService 类。

", @@ -3794,6 +4094,11 @@ "translation": "

MyValue 的服务提供商改写为一个特定的值 41

", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" }, + { + "original": "Routing and navigation", + "translation": "路由与导航", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/cheatsheet.md" + }, { "original": "

Configures routes for the application. Supports static, parameterized, redirect, and wildcard routes. Also supports custom route data and resolve.

", "translation": "

为该应用配置路由。支持静态、参数化、重定向和通配符路由。也支持自定义路由数据和解析(resolve)函数。

", @@ -3886,7 +4191,7 @@ }, { "original": "**See the **.", - "translation": "**参见**。", + "translation": "**参见**。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-interaction.md" }, { @@ -4519,6 +4824,11 @@ "translation": "我们可以指定多个样式文件,甚至可以组合使用 `style` 和 `styleUrls` 方式。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" }, + { + "original": "The CLI creates an empty styles file for you by default and references that file in the component's generated `styleUrls`.", + "translation": "CLI 会默认为你创建一个空白的样式表文件,并且在所生成组件的 `styleUrls` 中引用该文件。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, { "original": "### Template inline styles", "translation": "### 模板内联样式", @@ -4539,6 +4849,16 @@ "translation": "我们也可以在组件的 HTML 模板中写``标签。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" }, + { + "original": "The link tag's `href` URL must be relative to the\n_**application root**_, not relative to the component file.", + "translation": "link 标签的 `href` URL 必须是相对于***本应用的根路径***的,而不是相对于这个组件文件的。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "When building with the CLI, be sure to include the linked style file among the assets to be copied to the server as described in the [CLI documentation](https://github.com/angular/angular-cli/wiki/stories-asset-configuration).", + "translation": "当使用 CLI 进行构建时,要确保这个链接到的样式表文件被复制到了服务器上。参见 [CLI 官方文档](https://github.com/angular/angular-cli/wiki/stories-asset-configuration)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, { "original": "### CSS @imports", "translation": "### CSS @imports 语法", @@ -4554,6 +4874,51 @@ "translation": "在*这种*情况下,URL 是相对于我们执行导入操作的 CSS 文件的。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" }, + { + "original": "### External and global style files", + "translation": "### 外部以及全局样式文件", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "When building with the CLI, you must configure the `.angular-cli.json` to include _all external assets_, including external style files.", + "translation": "当使用 CLI 进行构建时,你必须配置 `.angular-cli.json` 文件,使其包含*所有外部资源*(包括外部的样式表文件)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "Register **global** style files in the `styles` section which, by default, is pre-configured with the global `styles.css` file.", + "translation": "在它的 `styles` 区注册这些**全局**样式文件,默认情况下,它会有一个预先配置的全局 `styles.css` 文件。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "See the [CLI documentation](https://github.com/angular/angular-cli/wiki/stories-global-styles) to learn more.", + "translation": "要了解更多,参见 [CLI 官方文档](https://github.com/angular/angular-cli/wiki/stories-global-styles)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "### Non-CSS style files", + "translation": "### 非 CSS 样式文件", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "If you're building with the CLI,\nyou can write style files in [sass](http://sass-lang.com/), [less](http://lesscss.org/), or [stylus](http://stylus-lang.com/) and specify those files in the `@Component.styleUrls` metadata with the appropriate extensions (`.scss`, `.less`, `.styl`) as in the following example:", + "translation": "如果使用 CLI 进行构建,那么你可以用 [sass](http://sass-lang.com/)、[less](http://lesscss.org/) 或 [stylus](http://stylus-lang.com/) 来编写样式,并使用相应的扩展名(`.scss`、`.less`、`.styl`)把它们指定到 `@Component.styleUrls` 元数据中。例子如下:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "The CLI build process runs the pertinent CSS preprocessor.", + "translation": "CLI 的构建过程会运行相关的预处理器。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "When generating a component file with `ng generate component`, the CLI emits an empty CSS styles file (`.css`) by default.\nYou can configure the CLI to default to your preferred CSS preprocessor\nas explained in the [CLI documentation](https://github.com/angular/angular-cli/wiki/stories-css-preprocessors\n\"CSS Preprocessor integration\").", + "translation": "当使用 `ng generate component` 命令生成组件文件时,CLI 会默认生成一个空白的 CSS 样式文件(`.css`)。\n你可以配置 CLI,让它默认使用你喜欢的 CSS 预处理器,参见 [CLI 官方文档](https://github.com/angular/angular-cli/wiki/stories-css-preprocessors\n \"CSS Preprocessor integration\") 中的解释。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, + { + "original": "Style strings added to the `@Component.styles` array _must be written in CSS_ because the CLI cannot apply a preprocessor to inline styles.", + "translation": "添加到 `@Component.styles` 数组中的字符串*必须写成 CSS*,因为 CLI 没法对这些内联的样式使用任何 CSS 预处理器。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/component-styles.md" + }, { "original": "## View encapsulation", "translation": "## 控制视图的封装模式:原生 (Native)、仿真 (Emulated) 和无 (None)", @@ -5729,6 +6094,31 @@ "translation": "我们使用`forwardRef`来打破这种循环:", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-in-action.md" }, + { + "original": "# The Dependency Injection pattern", + "translation": "# 依赖注入(Dependency injection)模式", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" + }, + { + "original": "**Dependency injection** is an important application design pattern.\nIt's used so widely that almost everyone just calls it _DI_.", + "translation": "**依赖注入**是一个很重要的设计模式。\n它使用得非常广泛,以至于几乎每个人都把它简称为 *DI* 。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" + }, + { + "original": "Angular has its own dependency injection framework, and\nyou really can't build an Angular application without it.", + "translation": "Angular 有自己的依赖注入框架,离开它,你几乎没办法构建出 Angular 应用。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" + }, + { + "original": "This page covers what DI is and why it's useful.", + "translation": "本页会告诉你 DI 是什么,以及为什么它很有用。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" + }, + { + "original": "When you've learned the general pattern, you're ready to turn to\nthe [Angular Dependency Injection](guide/dependency-injection) guide to see how it works in an Angular app.", + "translation": "当你学会了这种通用的模式之后,就可以转到 [Angular 依赖注入](guide/dependency-injection) 中去看看它在 Angular 应用中的工作原理了。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" + }, { "original": "## Why dependency injection?", "translation": "## 为什么需要依赖注入?", @@ -5739,6 +6129,11 @@ "translation": "要理解为什么依赖注入这么重要,不妨先考虑不使用它的一个例子。想象下列代码:", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" }, + { + "original": "The `Car` class creates everything it needs inside its constructor.\nWhat's the problem?\nThe problem is that the `Car` class is brittle, inflexible, and hard to test.", + "translation": "`Car` 类在自己的构造函数中创建了它所需的一切。\n这样做有什么问题?\n问题在于 `Car` 类是脆弱、不灵活以及难于测试的。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" + }, { "original": "This `Car` needs an engine and tires. Instead of asking for them,\nthe `Car` constructor instantiates its own copies from\nthe very specific classes `Engine` and `Tires`.", "translation": "`Car`类需要一个引擎 (engine) 和一些轮胎 (tire),它没有去请求现成的实例,\n而是在构造函数中用具体的`Engine`和`Tires`类实例化出自己的副本。", @@ -5780,7 +6175,7 @@ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" }, { - "original": "{@a ctor-injection}", + "original": "That's super easy. Change the `Car` constructor to a version with DI:", "translation": "答案非常简单。把`Car`的构造函数改造成使用 DI 的版本:", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" }, @@ -5874,6 +6269,136 @@ "translation": "这就是“**依赖注入框架**”存在的原因。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" }, + { + "original": "Now that you know what dependency injection is and appreciate its benefits,\nturn to the [Angular Dependency Injection](guide/dependency-injection) guide to see how it is implemented in Angular.", + "translation": "现在,你知道什么是依赖注入以及它有什么优点了吧?那就请到 [Angular 依赖注入](guide/dependency-injection) 中去看看它在 Angular 中是如何实现的。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection-pattern.md" + }, + { + "original": "# Angular Dependency Injection", + "translation": "# Angular 依赖注入", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "**Dependency Injection (DI)** is a way to create objects that depend upon other objects.\nA Dependency Injection system supplies the dependent objects (called the _dependencies_)\nwhen it creates an instance of an object.", + "translation": "**依赖注入(DI)**是用来创建对象及其依赖的其它对象的一种方式。\n当依赖注入系统创建某个对象实例时,会负责提供该对象所依赖的对象(称为该对象的*依赖*)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The [Dependency Injection pattern](guide/dependency-injection-pattern) page describes this general approach.\n_The guide you're reading now_ explains how Angular's own Dependency Injection system works.", + "translation": "[依赖注入模式](guide/dependency-injection-pattern)中讲解了这种通用的方法。\n*在这里*", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "## DI by example", + "translation": "## DI 的例子", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "You'll learn Angular Dependency Injection through a discussion of the sample app that accompanies this guide.\nRun the anytime.", + "translation": "在这篇指南中,你将会通过对一个范例应用的讨论来学习 Angular 的依赖注入技术。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Start by reviewing this simplified version of the _heroes_ feature\nfrom the [The Tour of Heroes](tutorial/).", + "translation": "我们先从[《英雄指南》](tutorial/)中*英雄*特性区的一个简化版本开始。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The `HeroesComponent` is the top-level heroes component.\nIt's only purpose is to display the `HeroListComponent`\nwhich displays a list of hero names.", + "translation": "`HeroesComponent` 是位于顶级的组件。\n它唯一的用途是显示 `HeroListComponent`,它显示一个英雄名字的列表。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "This version of the `HeroListComponent` gets its `heroes` from the `HEROES` array, an in-memory collection\ndefined in a separate `mock-heroes` file.", + "translation": "这个版本的 `HeroListComponent` 从 `HEROES` 数组(定义在 `mock-heroes` 文件中的内存数组)中获取 `heroes`。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "That may suffice in the early stages of development, but it's far from ideal.\nAs soon as you try to test this component or get heroes from a remote server,\nyou'll have to change the implementation of `HerosListComponent` and\nreplace every other use of the `HEROES` mock data.", + "translation": "在开发的早期阶段,这就够用了,不过还很不理想。\n当要测试这个组件或者要从远端服务器获取英雄数据时,你就不得不去修改 `HeroesListComponent` 的实现,并要替换所有使用了 `HEROES` 模拟数据的地方。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "It's better to hide these details inside a _service_ class, \n[defined in its own file](#one-class-per-file).", + "translation": "最好隐藏服务类的这些内部实现细节,那就先[把它定义在自己的文件中](#one-class-per-file)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "## Create an injectable _HeroService_", + "translation": "## 创建一个可注入的 `HeroService`", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The [**Angular CLI**](https://cli.angular.io/) can generate a new `HeroService` class in the `src/app/heroes` folder with this command.", + "translation": "[**Angular CLI**](https://cli.angular.io/) 可以使用下列命令在 `src/app/heroes` 目录下新建一个 `HeroService` 类。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "That command creates the following `HeroService` skeleton.", + "translation": "这条命令会创建如下的 `HeroService` 骨架代码:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Assume for now that the [`@Injectable` decorator](#injectable) is an essential ingredient in every Angular service definition.\nThe rest of the class has been rewritten to expose a `getHeroes` method \nthat returns the same mock data as before.", + "translation": "目前先把 [`@Injectable` 装饰器](#injectable)当做定义每个 Angular 服务时的必备部分。\n把该类的其它部分改写为暴露一个返回和以前一样的 mock 数据的 `getHeroes` 方法。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Of course, this isn't a real data service.\nIf the app were actually getting data from a remote server, \nthe `getHeroes` method signature would have to be asynchronous.", + "translation": "当然,这还不是真正的数据服务。\n如果该应用真的从远端服务器获取数据,那么 `getHeroes` 的方法签名就应该是异步形式的。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "That's a defect we can safely ignore in this guide where our focus is on\n_injecting the service_ into the `HeroList` component.", + "translation": "在这一章我们可以忽略这个问题,因为这里的焦点在于*把服务注入*到 `HeroListComponent` 组件中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "## Register a service provider", + "translation": "## 注册服务提供商", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "A _service_ is just a class in Angular until you register it with an Angular dependency injector.", + "translation": "在你把 Angular 中的*服务*注册进依赖注入器(injector)之前,它只是一个普通的类。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "An Angular injector is responsible for creating service instances and injecting them into classes like the `HeroListComponent`.", + "translation": "Angular 的依赖注入器负责创建服务的实例,并把它们注入到像 `HeroListComponent` 这样的类中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "You rarely create an Angular injector yourself.\nAngular creates injectors for you as it executes the app,\nstarting with the _root injector_ that it creates during the [bootstrap process](guide/bootstrapping).", + "translation": "你很少需要自己创建 Angular 的依赖注入器。\n当 Angular 运行本应用时,它会为你创建这些注入器,首先会在[引导过程](guide/bootstrapping)中创建一个*根注入器*。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "You do have to register _providers_ with an injector \nbefore the injector can create that service.", + "translation": "但在注入器能创建服务之前,你得先往注入器中注入这个服务的*提供商*。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "**Providers** tell the injector _how to create the service_.\nWithout a provider, the injector would not know\nthat it is responsible for injecting the service\nnor be able to create the service.", + "translation": "**提供商**会告诉注入器*如何创建该服务*。\n如果没有提供商,注入器既不知道它该负责创建该服务,也不知道如何创建该服务。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "You'll learn much more about _providers_ [below](#providers).\nFor now it is sufficient to know that they create services\nand must be registered with an injector.", + "translation": "你可以在[稍后的部分](#providers)学到更多关于*提供商*的知识。\n不过目前,只要知道它们用于创建服务,以及它们必须用注入器进行注册就行了。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "You can register a provider with any Angular decorator that supports the **`providers` array property**.", + "translation": "你可以使用 Angular 中那些支持 `providers` 数组属性的装饰器来注册提供商。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Many Angular decorators accept metadata with a `providers` property.\nThe two most important examples are `@Component` and `@NgModule`.", + "translation": "很多 Angular 的装饰器都支持带有 `providers` 属性的元数据。\n最重要的两个例子是 `@Component` 和 `@NgModule`。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, { "original": "### _@Component_ providers", "translation": "### 在组件中注册提供商", @@ -5884,6 +6409,166 @@ "translation": "下面是修改过的`HerosComponent`,把`HeroService`注册到了它的`providers`数组中。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" }, + { + "original": "### _@NgModule_ providers", + "translation": "### `@NgModule` 中的 `providers`", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "In the following excerpt, the root `AppModule` registers two providers in its `providers` array.", + "translation": "在下面的代码片段中,根模块 `AppModule` 在自己的 `providers` 数组中注册了两个提供商。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The first entry registers the `UserService` class (_not shown_) under the `UserService` _injection token_.\nThe second registers a value (`HERO_DI_CONFIG`) under the `APP_CONFIG` _injection token_.", + "translation": "第一条使用 `UserService` 这个*注入令牌(injection token)*注册了 `UserService` 类(代码中未显示)。\n第二条使用 `APP_CONFIG` 这个注入令牌注册了一个值(`HERO_DI_CONFIG`)。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Thanks to these registrations, Angular can inject the `UserService` or the `HERO_DI_CONFIG` value\ninto any class that it creates.", + "translation": "得益于这些注册语句,Angular 现在可以向它创建的任何类中注册 `UserService` 或 `HERO_DI_CONFIG` 值了。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "You'll learn about _injection tokens_ and _provider_ syntax [below](#providers).", + "translation": "[稍后](#providers)你就会学到关于*注入令牌*和服务提供商语法的知识。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "### _@NgModule_ or _@Component_?", + "translation": "### _@NgModule_ 还是 _@Component_?", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Should you register a service with an Angular module or with a component?\nThe two choices lead to differences in service _scope_ and service _lifetime_.", + "translation": "你该使用 Angular 的模块还是组件来注册服务呢?\n这两个选择的差别在于服务的*范围*和*生命周期*。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "**Angular module providers** (`@NgModule.providers`) are registered with the application's root injector.\nAngular can inject the corresponding services in any class it creates.\nOnce created, a service instance lives for the life of the app and Angular injects this one service instance in every class that needs it.", + "translation": "**Angular 模块中的 `providers`**(`@NgModule.providers`)是注册在应用的根注入器下的。\n因此,Angular 可以往它所创建的任何类中注入相应的服务。 \n一旦创建,服务的实例就会存在于该应用的全部生存期中,Angular 会把这一个服务实例注入到需求它的每个类中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "You're likely to inject the `UserService` in many places throughout the app\nand will want to inject the same service instance every time.\nProviding the `UserService` with an Angular module is a good choice.", + "translation": "如果你想要把这个 `UserService` 注入到应用中的很多地方,并且期望每次注入的都是同一个服务实例,那么在 Angular 的模块中提供 `UserService` 就是不错的选择。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "To be precise, Angular module providers are registered with the root injector\n_unless the module is_ [lazy loaded](guide/lazy-loading-ngmodules).\nIn this sample, all modules are _eagerly loaded_ when the application starts,\nso all module providers are registered with the app's root injector.", + "translation": "严格来说,Angular 模块中的服务提供商会注册到根注入器上,但是,[惰性加载](guide/lazy-loading-ngmodules)的模块是例外。\n在这个例子中,所有模块都是在应用启动时*立即加载*的,因此模块上的所有服务提供商都注册到了应用的根注入器上。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "**A component's providers** (`@Component.providers`) are registered with each component instance's own injector.", + "translation": "**组件的提供商**(`@Component.providers`)会注册到每个组件实例自己的注入器上。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Angular can only inject the corresponding services in that component instance or one of its descendant component instances.\nAngular cannot inject the same service instance anywhere else.", + "translation": "因此 Angular 只能在该组件及其各级子组件的实例上注入这个服务实例,而不能在其它地方注入这个服务实例。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Note that a component-provided service may have a limited lifetime. Each new instance of the component gets its own instance of the service\nand, when the component instance is destroyed, so is that service instance.", + "translation": "注意,由组件提供的服务,也同样具有有限的生命周期。组件的每个实例都会有它自己的服务实例,并且,当组件实例被销毁的时候,服务的实例也同样会被销毁。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "In this sample app, the `HeroComponent` is created when the application starts\nand is never destroyed so the `HeroService` created for the `HeroComponent` also live for the life of the app.", + "translation": "在这个范例应用中,`HeroComponent` 会在应用启动时创建,并且它从未销毁,因此,由 `HeroComponent` 创建的 `HeroService` 也同样会活在应用的整个生命周期中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "If you want to restrict `HeroService` access to the `HeroesComponent` and its nested `HeroListComponent`,\nproviding the `HeroService` in the `HeroComponent` may be a good choice.", + "translation": "如果你要把 `HeroService` 的访问权限定在 `HeroesComponent` 及其嵌套的 `HeroListComponent` 中,那么在 `HeroesComponent` 中提供这个 `HeroService` 就是一个好选择。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The scope and lifetime of component-provided services is a consequence of [the way Angular creates component instances](#component-child-injectors).", + "translation": "由组件提供的服务,其范围和生命周期是 [Angular 如何创建组件实例](#component-child-injectors) 的必然结果。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "## Inject a service", + "translation": "## 注入某个服务", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The `HeroListComponent` should get heroes from the `HeroService`.", + "translation": "`HeroListComponent` 应该从 `HeroService` 中获取这些英雄数据。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The component shouldn't create the `HeroService` with `new`.\nIt should ask for the `HeroService` to be injected.", + "translation": "该组件不应该使用 `new` 来创建 `HeroService`。\n它应该要求注入 `HeroService`。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "You can tell Angular to inject a dependency in the component's constructor by specifying a **constructor parameter with the dependency type**.\nHere's the `HeroListComponent` constructor, asking for the `HeroService` to be injected.", + "translation": "你可以通过**在构造函数中添加一个带有该依赖类型的参数**来要求 Angular 把这个依赖注入到组件的构造函数中。\n下面是 `HeroListComponent` 的构造函数,它要求注入 `HeroService`。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Of course, the `HeroListComponent` should do something with the injected `HeroService`.\nHere's the revised component, making use of the injected service, side-by-side with the previous version for comparison.", + "translation": "当然,`HeroListComponent` 还应该使用注入的这个 `HeroService` 做点什么。\n下面输出修改过的组件,改用注入的服务,与前一个版本对比一下。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Notice that the `HeroListComponent` doesn't know where the `HeroService` comes from.\n_You_ know that it comes from the parent `HeroesComponent`.\nBut if you decided instead to provide the `HeroService` in the `AppModule`,\nthe `HeroListComponent` wouldn't change at all.\nThe _only thing that matters_ is that the `HeroService` is provided in some parent injector.", + "translation": "注意,`HeroListComponent` 并不知道 `HeroService` 来自哪里。\n当然*你自己*知道它来自父组件 `HeroesComponent`。\n但是如果你决定改在 `AppModule` 中提供 `HeroService`,`HeroListComponent` 不用做任何修改。\n它*唯一需要关心的事情*是 `HeroService` 是由某个父注入器提供的。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "## Singleton services", + "translation": "## 单例服务", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Services are singletons _within the scope of an injector_.\nThere is at most one instance of a service in a given injector.", + "translation": "服务*在每个注入器的范围内*是单例的。\n在任何一个注入器中,最多只会有同一个服务的一个实例。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "There is only one root injector and the `UserService` is registered with that injector.\nTherefore, there can be just one `UserService` instance in the entire app\nand every class that injects `UserService` get this service instance.", + "translation": "这里只有一个根注入器,而 `UserService` 就是在该注入器中注册的。\n所以,在整个应用中只能有一个 `UserService` 实例,每个要求注入 `UserService` 的类都会得到这个服务实例。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "However, Angular DI is a \n[hierarchical injection system](guide/hierarchical-dependency-injection), \nwhich means that nested injectors can create their own service instances.\nAngular creates nested injectors all the time.", + "translation": "不过,Angular DI 是一个 [多级注入系统](guide/hierarchical-dependency-injection),这意味着各级注入器都可以创建它们自己的服务实例。\nAngular 总会创建多级注入器。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "## Component child injectors", + "translation": "## 组件的子注入器", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "For example, when Angular creates a new instance of a component that has `@Component.providers`,\nit also creates a new _child injector_ for that instance.", + "translation": "例如,当 Angular 创建一个带有 `@Component.providers` 的组件实例时,也会同时为这个实例创建一个新的*子注入器*。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Component injectors are independent of each other and\neach of them creates its own instances of the component-provided services.", + "translation": "组件注入器是彼此独立的,每一个都会为这些组件提供的服务创建单独的实例。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "When Angular destroys one of these component instance, it also destroys the\ncomponent's injector and that injector's service instances.", + "translation": "当 Angular 销毁任何一个组件实例时,也会同时销毁组件的注入器以及该注入器中的那些服务实例。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Thanks to [injector inheritance](guide/hierarchical-dependency-injection),\nyou can still inject application-wide services into these components.\nA component's injector is a child of its parent component's injector,\nand a descendent of its parent's parent's injector, and so on all the way back to the application's _root_ injector.\nAngular can inject a service provided by any injector in that lineage.", + "translation": "在[注入器继承机制](guide/hierarchical-dependency-injection)的帮助下,你仍然可以把全应用级的服务注入到这些组件中。\n组件的注入器也是其父组件的注入器的子注入器,这同样适用于其父组件的父组件的注入器,以此类推,最终会回到应用的*根*注入器。\nAngular 可以注入由这个注入器谱系提供的任何一个注入器。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "For example, Angular could inject a `HeroListComponent`\nwith both the `HeroService` provided in `HeroComponent`\nand the `UserService` provided in `AppModule`.", + "translation": "比如,Angular 可以把由 `HeroComponent` 提供的 `HeroService` 和由 `AppModule` 提供的 `UserService` 注入到 `HeroService` 中。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, { "original": "## Testing the component", "translation": "## 测试组件", @@ -5919,11 +6604,91 @@ "translation": "如果它也有依赖,该怎么办呢?例如,它需要通过日志服务来汇报自己的活动。\n我们同样用*构造函数注入*模式,来添加一个带有`Logger`参数的构造函数。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" }, + { + "original": "Here is the revised `HeroService` that injects the `Logger`, side-by-side with the previous service for comparison.", + "translation": "下面是修改后的 `HeroService`,它注入了 `Logger`,对比前后这两个版本:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, { "original": "The constructor asks for an injected instance of a `Logger` and stores it in a private field called `logger`.\nThe `getHeroes()` method logs a message when asked to fetch heroes.", "translation": "这个构造函数要求注入一个`Logger`类的实例,并把它存到名为`logger`的私有字段中。\n 当请求英雄数据时,`getHeroes()`中就会记录一个消息。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" }, + { + "original": "#### The dependent _Logger_ service", + "translation": "#### 被依赖的 `Logger` 服务", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The sample app's `Logger` service is quite simple:", + "translation": "这个范例应用的 `Logger` 服务非常简单:", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "If the app didn't provide this `Logger`,\nAngular would throw an exception when it looked for a `Logger` to inject\ninto the `HeroService`.", + "translation": "如果该应用没有提供这个 `Logger` 服务,当 Angular 试图把 `Logger` 注入到 `HeroService` 中时,就会抛出一个异常。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Because a singleton logger service is useful everywhere,\nit's provided in the root `AppModule`.", + "translation": "因为 `Logger` 服务的单例应该随处可用,所以要在根模块 `AppModule` 中提供它。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The **[@Injectable()](api/core/Injectable)** decorator identifies a service class \nthat _might_ require injected dependencies.", + "translation": "**[@Injectable()](api/core/Injectable)** 装饰器表示*可能*需要往这个服务类中注入其它依赖。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The `HeroService` must be annotated with `@Injectable()` because it requires an injected `Logger`.", + "translation": "`HeroService` 必须带有 `@Injectable()` 装饰器,因为它需要把 `Logger` 注入进来。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Always write `@Injectable()` with parentheses, not just `@Injectable`.", + "translation": "写 `@Injectable()` 时必须带括号,不能只写 `@Injectable`。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "When Angular creates a class whose constructor has parameters,\nit looks for type and injection metadata about those parameters\nso that it can inject the right service.", + "translation": "当 Angular 要创建一个构造函数中带参数的类时,会先查找这些参数的类型,以便根据这些参数的元数据注入正确的服务。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "If Angular can't find that parameter information, it throws an error.", + "translation": "如果不能找到该参数的信息,Angular 就会报错。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Angular can only find the parameter information _if the class has a decorator of some kind_.\nWhile _any_ decorator will do,\nthe `@Injectable()` decorator is the standard decorator for service classes.", + "translation": "Angular 只能在*带有某种装饰器的类*上查找参数信息。*任何*装饰器都可以,而 `@Injectable()` 装饰器是各种服务类的标准装饰器。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The decorator requirement is imposed by TypeScript.", + "translation": "之所以必须有装饰器,是因为 TypeScript 强制要求的。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "TypeScript normally discards parameter type information when it _transpiles_ the code to JavaScript.\nIt preserves this information if the class has a decorator\nand the `emitDecoratorMetadata` compiler option is set `true` \nin TypeScript's `tsconfig.json` configuration file, .", + "translation": "当把 TypeScript 转译成 JavaScript 时,通常会丢弃参数的类型信息。\n但当该类带有装饰器并且当 `tsconfig.json` 配置文件中的 `emitDecoratorMetadata` 编译选项为 `true` 时,它就会保留这些信息。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The CLI configures `tsconfig.json` with `emitDecoratorMetadata: true`\nIt's your job to put `@Injectable()` on your service classes.", + "translation": "CLI 生成的 `tsconfig.json` 中已经有 `emitDecoratorMetadata: true` 选项了,你只要把 `@Injectable()` 加到你的服务类上就可以了。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "The `Logger` service is annotated with `@Injectable()` decorator too, \nalthough it has no constructor and no dependencies.", + "translation": "`Logger` 服务也带有 `@Injectable()` 装饰器,不过它没有构造器,也没有依赖。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "In fact, _every_ Angular service class in this app is annotated with the `@Injectable()` decorator, whether or not it has a constructor and dependencies.\n`@Injectable()` is a required coding style for services.", + "translation": "该应用中的*每个* Angular 服务类不管有没有构造器和依赖,都带有 `@Injectable()` 装饰器。\n事实上,`@Injectable()` 是风格指南中对服务类的要求。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, { "original": "## Providers", "translation": "## 服务提供商们", @@ -5939,6 +6704,21 @@ "translation": "必须为注入器注册一个服务的*提供商*,否则它就不知道该如何创建该服务。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" }, + { + "original": "The next few sections explain the many ways you can specify a provider.", + "translation": "在下面的几节中会解释指定提供商的多种方式。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "Almost all of the accompanying code snippets are extracts from the sample app's `providers.component.ts` file.", + "translation": "几乎所有的代码片段都是从范例应用的 `providers.component.ts` 文件中提取出来的。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, + { + "original": "### The class as its own provider", + "translation": "### 把类作为它自己的提供商", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, { "original": "There are many ways to *provide* something that looks and behaves like a `Logger`.\nThe `Logger` class itself is an obvious and natural provider.", "translation": "有很多方式可以*提供*一些实现 `Logger`类的东西。\n `Logger`类本身是一个显而易见而且自然而然的提供商。", @@ -5964,6 +6744,11 @@ "translation": "### *provide* 对象字面量", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" }, + { + "original": "Here's the class-provider syntax again.", + "translation": "下面是类提供商的另一种语法。", + "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dependency-injection.md" + }, { "original": "This is actually a shorthand expression for a provider registration\nusing a _provider_ object literal with two properties:", "translation": "这其实是用于注册提供商的简写表达式。\n 使用的是一个带有两个属性的_提供商_对象字面量:", @@ -7216,7 +8001,7 @@ }, { "original": "See the .", - "translation": "参见。", + "translation": "参见。", "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/dynamic-component-loader.md" }, { @@ -8101,7 +8886,7 @@ }, { "original": "This code repeats the `