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": "如果设置了,那么 templateUrl
和 styleUrl
的路径就会相对于当前组件进行解析。
",
@@ -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 `` tag for each power in the list of powers.\nThe `pow` template input variable is a different power in each iteration;\nyou display its name using the interpolation syntax.",
- "translation": "列表中的每一项超能力都会渲染成` `标签。\n模板输入变量`p`在每个迭代指向不同的超能力,使用双花括号插值表达式语法来显示它的名称。",
+ "translation": "列表中的每一项超能力都会渲染成` `标签。\n模板输入变量`pow`在每个迭代指向不同的超能力,使用双花括号插值表达式语法来显示它的名称。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/forms.md"
},
{
@@ -8156,7 +8941,7 @@
},
{
"original": "### The _NgForm_ directive",
- "translation": "### `NgForm`指令",
+ "translation": "### *NgForm* 指令",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/forms.md"
},
{
@@ -9949,11 +10734,6 @@
"translation": "*车辆*场景下的代码位于`car.components.ts`和`car.services.ts`文件中,这个例子你可以在 查看和下载。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/hierarchical-dependency-injection.md"
},
- {
- "original": "# HttpClient",
- "translation": "# HttpClient 库",
- "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
- },
{
"original": "Most front-end applications communicate with backend services over the HTTP protocol. Modern browsers support two different APIs for making HTTP requests: the `XMLHttpRequest` interface and the `fetch()` API.",
"translation": "大多数前端应用都需要通过 HTTP 协议与后端服务器通讯。现代浏览器支持使用两种不同的 API 发起 HTTP 请求:`XMLHttpRequest` 接口和 `fetch()` API。",
@@ -9964,11 +10744,126 @@
"translation": "`@angular/common/http`中的`HttpClient`类为 Angular 应用程序提供了一个简化的 API 来实现 HTTP 客户端功能。它基于浏览器提供的`XMLHttpRequest`接口。\n`HttpClient`带来的其它优点包括:可测试性、强类型的请求和响应对象、发起请求与接收响应时的拦截器支持,以及更好的、基于可观察(Observable)对象的 API 以及流式错误处理机制。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "You can run the that accompanies this guide.",
+ "translation": "你可以到 中运行本章的代码。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The sample app does not require a data server.\nIt relies on the \n[Angular _in-memory-web-api_](https://github.com/angular/in-memory-web-api/blob/master/README.md),\nwhich replaces the _HttpClient_ module's `HttpBackend`.\nThe replacement service simulates the behavior of a REST-like backend.",
+ "translation": "该应用代码并不需要数据服务器。\n它基于 [Angular _in-memory-web-api_](https://github.com/angular/in-memory-web-api/blob/master/README.md) 库,该库会替换 `HttpClient` 模块中的 `HttpBackend`。用于替换的这个服务会模拟 REST 风格的后端的行为。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Look at the `AppModule` _imports_ to see how it is configured.",
+ "translation": "到 `AppModule` 的 `imports` 中查看这个库是如何配置的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "## Setup",
"translation": "## 准备工作",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Before you can use the `HttpClient`, you need to import the Angular `HttpClientModule`. \nMost apps do so in the root `AppModule`.",
+ "translation": "要想使用 `HttpClient`,就要先导入 Angular 的 `HttpClientModule`。大多数应用都会在根模块 `AppModule` 中导入它。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Having imported `HttpClientModule` into the `AppModule`, you can inject the `HttpClient`\ninto an application class as shown in the following `ConfigService` example.",
+ "translation": "在 `AppModule` 中导入 `HttpClientModule` 之后,你可以把 `HttpClient` 注入到应用类中,就像下面的 `ConfigService` 例子中这样。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "## Getting JSON data",
+ "translation": "## 获取 JSON 数据",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Applications often request JSON data from the server. \nFor example, the app might need a configuration file on the server, `config.json`, \nthat specifies resource URLs.",
+ "translation": "应用通常会从服务器上获取 JSON 数据。\n比如,该应用可能要从服务器上获取配置文件 `config.json`,其中指定了一些特定资源的 URL。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `ConfigService` fetches this file with a `get()` method on `HttpClient`.",
+ "translation": "`ConfigService` 会通过 `HttpClient` 的 `get()` 方法取得这个文件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "A component, such as `ConfigComponent`, injects the `ConfigService` and calls\nthe `getConfig` service method.",
+ "translation": "像 `ConfigComponent` 这样的组件会注入 `ConfigService`,并调用其 `getConfig` 方法。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Because the service method returns an `Observable` of configuration data,\nthe component **subscribes** to the method's return value.\nThe subscription callback copies the data fields into the component's `config` object,\nwhich is data-bound in the component template for display.",
+ "translation": "这个服务方法返回配置数据的 `Observable` 对象,所以组件要**订阅(subscribe)** 该方法的返回值。\n订阅时的回调函数会把这些数据字段复制到组件的 `config` 对象中,它会在组件的模板中绑定,以供显示。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Why write a service",
+ "translation": "### 为什么要写服务",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "This example is so simple that it is tempting to write the `Http.get()` inside the\ncomponent itself and skip the service.",
+ "translation": "这个例子太简单,所以它也可以在组件本身的代码中调用 `Http.get()`,而不用借助服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "However, data access rarely stays this simple.\nYou typically post-process the data, add error handling, and maybe some retry logic to\ncope with intermittent connectivity.",
+ "translation": "不过,数据访问很少能一直这么简单。\n你通常要对数据做后处理、添加错误处理器,还可能加一些重试逻辑,以便应对网络抽风的情况。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The component quickly becomes cluttered with data access minutia.\nThe component becomes harder to understand, harder to test, and the data access logic can't be re-used or standardized.",
+ "translation": "该组件很快就会因为这些数据方式的细节而变得杂乱不堪。\n组件变得难以理解、难以测试,并且这些数据访问逻辑无法被复用,也无法标准化。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "That's why it is a best practice to separate presentation of data from data access by\nencapsulating data access in a separate service and delegating to that service in\nthe component, even in simple cases like this one.",
+ "translation": "这就是为什么最佳实践中要求把数据展现逻辑从数据访问逻辑中拆分出去,也就是说把数据访问逻辑包装进一个单独的服务中,\n并且在组件中把数据访问逻辑委托给这个服务。就算是这么简单的应用也要如此。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Type-checking the response",
+ "translation": "### 带类型检查的响应",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The subscribe callback above requires bracket notation to extract the data values.",
+ "translation": "该订阅的回调需要用通过括号中的语句来提取数据的值。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You can't write `data.heroesUrl` because TypeScript correctly complains that the `data` object from the service does not have a `heroesUrl` property.",
+ "translation": "你没法写成 `data.heroesUrl` ,因为 TypeScript 会报告说来自服务器的 `data` 对象中没有名叫 `heroesUrl` 的属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `HttpClient.get()` method parsed the JSON server response into the anonymous `Object` type. It doesn't know what the shape of that object is.",
+ "translation": "这是因为 `HttpClient.get()` 方法把 JSON 响应体解析成了匿名的 `Object` 类型。它不知道该对象的具体形态如何。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You can tell `HttpClient` the type of the response to make consuming the output easier and more obvious.",
+ "translation": "你可以告诉 `HttpClient` 该响应体的类型,以便让对这种输出的消费更容易、更明确。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "First, define an interface with the correct shape:",
+ "translation": "首先,定义一个具有正确形态的接口:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Then, specify that interface as the `HttpClient.get()` call's type parameter in the service:",
+ "translation": "然后,在服务器中把该接口指定为 `HttpClient.get()` 调用的类型参数。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The callback in the updated component method receives a typed data object, which is\neasier and safer to consume:",
+ "translation": "修改后的组件方法,其回调函数中获取一个带类型的对象,它易于使用,且消费起来更安全:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "### Reading the full response",
"translation": "### 读取完整的响应体",
@@ -9984,6 +10879,21 @@
"translation": "要这样做,我们就要通过`observe`选项来告诉`HttpClient`,你想要完整的响应信息,而不是只有响应体:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Now `HttpClient.get()` returns an `Observable` of typed `HttpResponse` rather than just the JSON data.",
+ "translation": "现在 `HttpClient.get()` 会返回一个 `HttpResponse` 类型的 `Observable`,而不只是 JSON 数据。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The component's `showConfigResponse()` method displays the response headers as well as the configuration:",
+ "translation": "该组件的 `showConfigResponse()` 方法会像显示配置数据一样显示响应头:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "As you can see, the response object has a `body` property of the correct type.",
+ "translation": "如你所见,该响应对象具有一个带有正确类型的 `body` 属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "## Error handling",
"translation": "## 错误处理",
@@ -9994,6 +10904,16 @@
"translation": "如果这个请求导致了服务器错误怎么办?甚至,在烂网络下请求都没到服务器该怎么办?`HttpClient`就会返回一个错误(error)而不再是成功的响应。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "You _could_ handle in the component by adding a second callback to the `.subscribe()`:",
+ "translation": "通过在 `.subscribe()` 中添加第二个回调函数,你*可以*在组件中处理它:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "It's certainly a good idea to give the user some kind of feedback when data access fails.\nBut displaying the raw error object returned by `HttpClient` is far from the best way to do it.",
+ "translation": "在数据访问失败时给用户一些反馈,确实是个好主意。\n不过,直接显示由 `HttpClient` 返回的原始错误数据还远远不够。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "### Getting error details",
"translation": "### 获取错误详情",
@@ -10011,7 +10931,97 @@
},
{
"original": "Or something could go wrong on the client-side such as a network error that prevents the request from completing successfully or an exception thrown in an RxJS operator. These errors produce JavaScript `ErrorEvent` objects.",
- "translation": "或者,如果在客户端这边出了错误(比如在RxJS操作符中抛出的异常或某些阻碍完成这个请求的网络错误),就会抛出一个`Error`类型的异常。",
+ "translation": "或者,如果在客户端这边出了错误(比如在RxJS操作符 (operator) 中抛出的异常或某些阻碍完成这个请求的网络错误),就会抛出一个`Error`类型的异常。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `HttpClient` captures both kinds of errors in its `HttpErrorResponse` and you can inspect that response to figure out what really happened.",
+ "translation": "`HttpClient` 会在 `HttpErrorResponse` 中捕获所有类型的错误信息,你可以查看这个响应体以了解到底发生了什么。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Error inspection, interpretation, and resolution is something you want to do in the _service_, \nnot in the _component_.",
+ "translation": "错误的探查、解释和解决是你应该在*服务*中做的事情,而不是在*组件*中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You might first devise an error handler like this one:",
+ "translation": "你可能首先要设计一个错误处理器,就像这样:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Notice that this handler returns an RxJS [`ErrorObservable`](#rxjs) with a user-friendly error message.\nConsumers of the service expect service methods to return an `Observable` of some kind,\neven a \"bad\" one.",
+ "translation": "注意,该处理器返回一个带有用户友好的错误信息的 RxJS [`ErrorObservable`](#rxjs) 对象。\n该服务的消费者期望服务的方法返回某种形式的 `Observable`,就算是“错误的”也可以。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Now you take the `Observables` returned by the `HttpClient` methods\nand _pipe them through_ to the error handler.",
+ "translation": "现在,你获取了由 `HttpClient` 方法返回的 `Observable`,并*把它们通过管道*传给错误处理器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Sometimes the error is transient and will go away automatically if you try again.\nFor example, network interruptions are common in mobile scenarios, and trying again\nmay produce a successful result.",
+ "translation": "有时候,错误只是临时性的,只要重试就可能会自动消失。\n比如,在移动端场景中可能会遇到网络中断的情况,只要重试一下就能拿到正确的结果。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The [RxJS library](#rxjs) offers several _retry_ operators that are worth exploring.\nThe simplest is called `retry()` and it automatically re-subscribes to a failed `Observable` a specified number of times. _Re-subscribing_ to the result of an `HttpClient` method call has the effect of reissuing the HTTP request.",
+ "translation": "[RxJS 库](#rxjs)提供了几个 `retry` 操作符,它们值得仔细看看。\n其中最简单的是 `retry()`,它可以对失败的 `Observable` 自动重新订阅几次。对 `HttpClient` 方法调用的结果进行*重新订阅*会导致重新发起 HTTP 请求。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "_Pipe_ it onto the `HttpClient` method result just before the error handler.",
+ "translation": "把它插入到 `HttpClient` 方法结果的*管道*中,就放在错误处理器的紧前面。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "## Observables and operators",
+ "translation": "## 可观察对象 (Observable) 与操作符 (operator)",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The previous sections of this guide referred to RxJS `Observables` and operators such as `catchError` and `retry`.\nYou will encounter more RxJS artifacts as you continue below.",
+ "translation": "本章的前一节中引用了 RxJS 的 `Observable` 和 `operator`,比如 `catchError` 和 `retry`。\n接下来你还会遇到更多 RxJS 中的概念。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "[RxJS](http://reactivex.io/rxjs/) is a library for composing asynchronous and callback-based code\nin a _functional, reactive style_.\nMany Angular APIs, including `HttpClient`, produce and consume RxJS `Observables`.",
+ "translation": "[RxJS](http://reactivex.io/rxjs/) 是一个库,用于把异步调用和基于回调的代码组合成*函数式(functional)的*、*响应式(reactive)的*风格。\n很多 Angular API,包括 `HttpClient`都会生成和消费 RxJS 的 `Observable`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "RxJS itself is out-of-scope for this guide. You will find many learning resources on the web.\nWhile you can get by with a minimum of RxJS knowledge, you'll want to grow your RxJS skills over time in order to use `HttpClient` effectively.",
+ "translation": "RxJS 本身超出了本章的范围。你可以在网络上找到更多的学习资源。\n虽然只用少量的 RxJS 知识就可以获得解决方案,不过以后你会逐步提高 RxJS 技能,以便更高效的使用 `HttpClient`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If you're following along with these code snippets, note that you must import the RxJS observable and operator symbols that appear in those snippets. These `ConfigService` imports are typical.",
+ "translation": "如果你在跟着教程敲下面这些代码片段,要注意你要自己导入这里出现的 RxJS 的可观察对象和操作符。就像 `ConfigService` 中的这些导入。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "## Requesting non-JSON data",
+ "translation": "## 请求非 JSON 格式的数据",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Not all APIs return JSON data. In this next example,\na `DownloaderService` method reads a text file from the server\nand logs the file contents, before returning those contents to the caller\nas an `Observable`.",
+ "translation": "不是所有的 API 都会返回 JSON 数据。在下面这个例子中,`DownloaderService` 中的方法会从服务器读取文本文件,\n并把文件的内容记录下来,然后把这些内容使用 `Observable` 的形式返回给调用者。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "`HttpClient.get()` returns a string rather than the default JSON because of the `responseType` option.",
+ "translation": "这里的 `HttpClient.get()` 返回字符串而不是默认的 JSON 对象,因为它的 `responseType` 选项是 `'text'`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The RxJS `tap` operator (as in \"wiretap\") lets the code inspect good and error values passing through the observable without disturbing them.",
+ "translation": "RxJS 的 `tap` 操作符(可看做 wiretap - 窃听),让这段代码探查由可观察对象传过来的正确值和错误值,而不用打扰它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "A `download()` method in the `DownloaderComponent` initiates the request by subscribing to the service method.",
+ "translation": "在 `DownloaderComponent` 中的 `download()` 方法通过订阅这个服务中的方法来发起一次请求。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
{
@@ -10019,21 +11029,566 @@
"translation": "## 把数据发送到服务器",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "In addition to fetching data from the server, `HttpClient` supports mutating requests, that is, sending data to the server with other HTTP methods such as PUT, POST, and DELETE.",
+ "translation": "除了从服务器获取数据之外,`HttpClient` 还支持修改型的请求,也就是说,通过`PUT`、`POST`、`DELETE` 这样的 HTTP 方法把数据发送到服务器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The sample app for this guide includes a simplified version of the \"Tour of Heroes\" example\nthat fetches heroes and enables users to add, delete, and update them.",
+ "translation": "本指南中的这个范例应用包括一个简化版本的《英雄指南》,它会获取英雄数据,并允许用户添加、删除和修改它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The following sections excerpt methods of the sample's `HeroesService`.",
+ "translation": "下面的这些章节中包括该范例的 `HeroesService` 中的一些方法片段。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Adding headers",
+ "translation": "### 添加请求头",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Many servers require extra headers for save operations.\nFor example, they may require a \"Content-Type\" header to explicitly declare \nthe MIME type of the request body.\nOr perhaps the server requires an authorization token.",
+ "translation": "很多服务器在进行保存型操作时需要额外的请求头。\n比如,它们可能需要一个 `Content-Type` 头来显式定义请求体的 MIME 类型。\n也可能服务器会需要一个认证用的令牌(token)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `HeroesService` defines such headers in an `httpOptions` object that will be passed\nto every `HttpClient` save method.",
+ "translation": "`HeroesService` 在 `httpOptions` 对象中就定义了一些这样的请求头,并把它传给每个 `HttpClient` 的保存型方法。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "### Making a POST request",
"translation": "### 发起一个 POST 请求",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Apps often POST data to a server. They POST when submitting a form. \nIn the following example, the `HeroService` posts when adding a hero to the database.",
+ "translation": "应用经常把数据 `POST` 到服务器。它们会在提交表单时进行 `POST`。\n下面这个例子中,`HeroService` 在把英雄添加到数据库中时,就会使用 `POST`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `HttpClient.post()` method is similar to `get()` in that it has a type parameter\n(you're expecting the server to return the new hero)\nand it takes a resource URL.",
+ "translation": "`HttpClient.post()` 方法像 `get()` 一样也有类型参数(你会希望服务器返回一个新的英雄对象),它包含一个资源 URL。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "It takes two more parameters:",
+ "translation": "它还接受另外两个参数:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "1. `hero` - the data to POST in the body of the request.",
+ "translation": "`hero` - 要 `POST` 的请求体数据。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "1. `httpOptions` - the method options which, in this case, [specify required headers](#adding-headers).",
+ "translation": "`httpOptions` - 这个例子中,该方法的选项[指定了所需的请求头](#adding-headers)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Of course it catches errors in much the same manner [described above](#error-details).\nIt also _taps_ the returned observable in order to log the successful POST.",
+ "translation": "当然,它捕获错误的方式很像[前面描述的](#error-details)操作方式。\n它还*窃听*了可观察对象的返回值,以记录成功的 `POST`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `HeroesComponent` initiates the actual POST operation by subscribing to \nthe `Observable` returned by this service method.",
+ "translation": "`HeroesComponent` 通过订阅该服务方法返回的 `Observable` 发起了一次实际的 `POST` 操作。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "When the server responds successfully with the newly added hero, the component adds\nthat hero to the displayed `heroes` list.",
+ "translation": "当服务器成功做出响应时,会带有这个新创建的英雄,然后该组件就会把这个英雄添加到正在显示的 `heroes` 列表中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Making a DELETE request",
+ "translation": "### 发起 `DELETE` 请求",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "This application deletes a hero with the `HttpClient.delete` method by passing the hero's id\nin the request URL.",
+ "translation": "该应用可以把英雄的 id 传给 `HttpClient.delete` 方法的请求 URL 来删除一个英雄。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `HeroesComponent` initiates the actual DELETE operation by subscribing to \nthe `Observable` returned by this service method.",
+ "translation": "当 `HeroesComponent` 订阅了该服务方法返回的 `Observable` 时,就会发起一次实际的 `DELETE` 操作。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You must call _subscribe()_ or nothing happens!",
+ "translation": "你必须调用 `subscribe()`,否则什么都不会发生。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The component isn't expecting a result from the delete operation and\nsubscribes without a callback.\nThe bare `.subscribe()` _seems_ pointless.",
+ "translation": "该组件不关心删除操作返回的结果,订阅时也没有回调函数。\n单纯的 `.subscribe()` 方法看似毫无意义。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "In fact, it is essential.\nMerely calling `HeroService.deleteHero()` **does not initiate the DELETE request.**",
+ "translation": "但实际上,它是必备的。\n否则调用 `HeroService.deleteHero()` 时**不会发起 DELETE 请求**。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Always _subscribe_!",
+ "translation": "### 别忘了*订阅*!",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "An `HttpClient` method does not begin its HTTP request until you call `subscribe()` on the observable returned by that method. This is true for _all_ `HttpClient` _methods_.",
+ "translation": "在调用方法返回的可观察对象的 `subscribe()` 方法之前,`HttpClient` 方法不会发起 HTTP 请求。这适用于 `HttpClient` 的*所有方法*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The [`AsyncPipe`](api/common/AsyncPipe) subscribes (and unsubscribes) for you automatically.",
+ "translation": "[`AsyncPipe`](api/common/AsyncPipe) 会自动为你订阅(以及取消订阅)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "All observables returned from `HttpClient` methods are _cold_ by design.\nExecution of the HTTP request is _deferred_, allowing you to extend the\nobservable with additional operations such as `tap` and `catchError`\n before anything actually happens.",
+ "translation": "`HttpClient` 的所有方法返回的可观察对象都设计为*冷的*。\nHTTP 请求的执行都是*延期执行的*,让你可以用 `tap` 和 `catchError` 这样的操作符来在实际执行什么之前,先对这个可观察对象进行扩展。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Calling `subscribe(...)` triggers execution of the observable and causes\n`HttpClient` to compose and send the HTTP request to the server.",
+ "translation": "调用 `subscribe(...)` 会触发这个可观察对象的执行,并导致 `HttpClient` 组合并把 HTTP 请求发给服务器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You can think of these observables as _blueprints_ for actual HTTP requests.",
+ "translation": "你可以把这些可观察对象看做实际 HTTP 请求的*蓝图*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "In fact, each `subscribe()` initiates a separate, independent execution of the observable.\nSubscribing twice results in two HTTP requests.",
+ "translation": "实际上,每个 `subscribe()` 都会初始化此可观察对象的一次单独的、独立的执行。\n订阅两次就会导致发起两个 HTTP 请求。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Making a PUT request",
+ "translation": "### 发起 PUT 请求",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "An app will send a PUT request to completely replace a resource with updated data.\nThe following `HeroService` example is just like the POST example.",
+ "translation": "应用可以发送 PUT 请求,来使用修改后的数据完全替换掉一个资源。\n下面的 `HeroService` 例子和 POST 的例子很像。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "For the reasons [explained above](#always-subscribe), the caller (`HeroesComponent.update()` in this case) must `subscribe()` to the observable returned from the `HttpClient.put()`\nin order to initiate the request.",
+ "translation": "因为[前面解释过的](#always-subscribe)原因,调用者(这里是`HeroesComponent.update()`)必须 `subscribe()` 由 `HttpClient.put()` 返回的可观察对象,以发起这个调用。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "## Advanced usage",
"translation": "## 高级用法",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "The above sections detail how to use the basic HTTP functionality in `@angular/common/http`, but sometimes you need to do more than make simple requests and get data back.",
+ "translation": "上面这个小节中详细讲解了如何使用 `@angular/common/http` 中提供的基本 HTTP 功能,但有时候除了单纯发起请求和取回数据之外,你还要再做点别的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Configuring the request",
+ "translation": "### 配置请求",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Other aspects of an outgoing request can be configured via the options object\npassed as the last argument to the `HttpClient` method.",
+ "translation": "待发送请求的其它方面可以通过传给 `HttpClient` 方法最后一个参数中的配置对象进行配置。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You [saw earlier](#adding-headers) that the `HeroService` sets the default headers by\npassing an options object (`httpOptions`) to its save methods.\nYou can do more.",
+ "translation": "[以前你曾在](#adding-headers) `HeroService` 中通过在其保存方法中传入配置对象 `httpOptions` 设置过默认头。\n你还可以做更多。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "#### Update headers",
+ "translation": "#### 修改这些头",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You can't directly modify the existing headers within the previous options\nobject because instances of the `HttpHeaders` class are immutable.",
+ "translation": "你没法直接修改前述配置对象中的现有头,因为这个 `HttpHeaders` 类的实例是不可变的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Use the `set()` method instead. \nIt returns a clone of the current instance with the new changes applied.",
+ "translation": "改用 `set()` 方法代替。\n它会返回当前实例的一份克隆,其中应用了这些新修改。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Here's how you might update the authorization header (after the old token expired) \nbefore making the next request.",
+ "translation": "比如在发起下一个请求之前,如果旧的令牌已经过期了,你可能还要修改认证头。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "#### URL Parameters",
"translation": "#### URL 参数",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Adding URL search parameters works a similar way.\nHere is a `searchHeroes` method that queries for heroes whose names contain the search term.",
+ "translation": "添加 URL 搜索参数也与此类似。\n这里的 `searchHeroes` 方法会查询名字中包含搜索词的英雄列表。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If there is a search term, the code constructs an options object with an HTML URL encoded search parameter. If the term were \"foo\", the GET request URL would be `api/heroes/?name=foo`.",
+ "translation": "如果有搜索词,这段代码就会构造一个包含进行过 URL 编码的搜索词的选项对象。如果这个搜索词是“foo”,这个 GET 请求的 URL 就会是 `api/heroes/?name=foo`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `HttpParams` are immutable so you'll have to use the `set()` method to update the options.",
+ "translation": "`HttpParams` 是不可变的,所以你也要使用 `set()` 方法来修改这些选项。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Debouncing requests",
+ "translation": "### 请求的防抖(debounce)",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The sample includes an _npm package search_ feature.",
+ "translation": "这个例子还包含了*搜索 npm 包*的特性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "When the user enters a name in a search-box, the `PackageSearchComponent` sends\na search request for a package with that name to the NPM web api.",
+ "translation": "当用户在搜索框中输入名字时,`PackageSearchComponent` 就会把一个根据名字搜索包的请求发送给 NPM 的 web api。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Here's a pertinent excerpt from the template:",
+ "translation": "下面是模板中的相关代码片段:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `(keyup)` event binding sends every keystroke to the component's `search()` method.",
+ "translation": "`(keyup)` 事件绑定把每次击键都发送给了组件的 `search()` 方法。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Sending a request for every keystroke could be expensive.\nIt's better to wait until the user stops typing and then send a request.\nThat's easy to implement with RxJS operators, as shown in this excerpt.",
+ "translation": "如果每次击键都发送一次请求就太昂贵了。\n最好能等到用户停止输入时才发送请求。\n使用 RxJS 的操作符就能轻易实现它,参见下面的代码片段:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `searchText$` is the sequence of search-box values coming from the user.\nIt's defined as an RxJS `Subject`, which means it is an `Observable`\nthat can also produce values for itself by calling `next(value)`,\nas happens in the `search()` method.",
+ "translation": "`searchText$` 是一个序列,包含用户输入到搜索框中的所有值。\n它定义成了 RxJS 的 `Subject` 对象,这表示它是一个 `Observable`,同时还可以自行调用 `next(value)` 来产生值。\n`search()` 方法中就是这么做的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Rather than forward every `searchText` value directly to the injected `PackageSearchService`,\nthe code in `ngOnInit()` _pipes_ search values through three operators:",
+ "translation": "除了把每个 `searchText` 的值都直接转发给 `PackageSearchService` 之外,`ngOnInit()` 中的代码还通过下列三个操作符对这些搜索值进行*管道*处理:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "1. `debounceTime(500)` - wait for the user to stop typing (1/2 second in this case).",
+ "translation": "`debounceTime(500)` - 等待,直到用户停止输入(这个例子中是停止 1/2 秒)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "1. `distinctUntilChanged()` - wait until the search text changes.",
+ "translation": "`distinctUntilChanged()` - 等待,直到搜索内容发生了变化。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "1. `switchMap()` - send the search request to the service.",
+ "translation": "`switchMap()` - 把搜索请求发送给服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The code sets `packages$` to this re-composed `Observable` of search results.\nThe template subscribes to `packages$` with the [AsyncPipe](api/common/AsyncPipe)\nand displays search results as they arrive.",
+ "translation": "这些代码把 `packages$` 设置成了使用搜索结果组合出的 `Observable` 对象。\n模板中使用 [AsyncPipe](api/common/AsyncPipe) 订阅了 `packages$`,一旦搜索结果的值发回来了,就显示这些搜索结果。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "A search value reaches the service only if it's a new value and the user has stopped typing.",
+ "translation": "这样,只有当用户停止了输入且搜索值和以前不一样的时候,搜索值才会传给服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `withRefresh` option is explained [below](#cache-refresh).",
+ "translation": "[稍后](#cache-refresh) 解释了这个 `withRefresh` 选项。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `switchMap()` operator has three important characteristics.",
+ "translation": "这个 `switchMap()` 操作符有三个重要的特征:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "1. It takes a function argument that returns an `Observable`.\n`PackageSearchService.search` returns an `Observable`, as other data service methods do.",
+ "translation": "它的参数是一个返回 `Observable` 的函数。`PackageSearchService.search` 会返回 `Observable`,其它数据服务也一样。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "2. If a previous search request is still _in-flight_ (as when the connection is poor),\nit cancels that request and sends a new one.",
+ "translation": "如果以前的搜索结果仍然是*在途*状态(这会出现在慢速网络中),它会取消那个请求,并发起这个新的搜索。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "3. It returns service responses in their original request order, even if the\nserver returns them out of order.",
+ "translation": "它会按照原始的请求顺序返回这些服务的响应,而不用关心服务器实际上是以乱序返回的它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If you think you'll reuse this debouncing logic,\nconsider moving it to a utility function or into the `PackageSearchService` itself.",
+ "translation": "如果你觉得将来会复用这些防抖逻辑,\n可以把它移到单独的工具函数中,或者移到 `PackageSearchService` 中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "### Intercepting requests and responses",
+ "translation": "### 拦截请求和响应",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "_HTTP Interception_ is a major feature of `@angular/common/http`. \nWith interception, you declare _interceptors_ that inspect and transform HTTP requests from your application to the server.\nThe same interceptors may also inspect and transform the server's responses on their way back to the application.\nMultiple interceptors form a _forward-and-backward_ chain of request/response handlers.",
+ "translation": "*HTTP* 拦截机制是 `@angular/common/http` 中的主要特性之一。\n使用这种拦截机制,你可以声明*一些拦截器*,用它们监视和转换从应用发送到服务器的 HTTP 请求。\n拦截器还可以用监视和转换从服务器返回到本应用的那些响应。\n多个选择器会构成一个“请求/响应处理器”的双向链表。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Interceptors can perform a variety of _implicit_ tasks, from authentication to logging, in a routine, standard way, for every HTTP request/response.",
+ "translation": "拦截器可以用一种常规的、标准的方式对每一次 HTTP 的请求/响应任务执行从认证到记日志等很多种*隐式*任务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Without interception, developers would have to implement these tasks _explicitly_ \nfor each `HttpClient` method call.",
+ "translation": "如果没有拦截机制,那么开发人员将不得不对每次 `HttpClient` 调用*显式*实现这些任务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "#### Write an interceptor",
+ "translation": "#### 编写拦截器",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "To implement an interceptor, declare a class that implements the `intercept()` method of the `HttpInterceptor` interface.",
+ "translation": "要实现拦截器,就要实现一个实现了 `HttpInterceptor` 接口中的 `intercept()` 方法的类。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Here is a do-nothing _noop_ interceptor that simply passes the request through without touching it:",
+ "translation": "这里是一个什么也不做的*空白*拦截器,它只会不做任何修改的传递这个请求。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `intercept` method transforms a request into an `Observable` that eventually returns the HTTP response. \nIn this sense, each interceptor is fully capable of handling the request entirely by itself.",
+ "translation": "`intercept` 方法会把请求转换成一个最终返回 HTTP 响应体的 `Observable`。\n在这个场景中,每个拦截器都完全能自己处理这个请求。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Most interceptors inspect the request on the way in and forward the (perhaps altered) request to the `handle()` method of the `next` object which implements the [`HttpHandler`](api/common/http/HttpHandler) interface.",
+ "translation": "大多数拦截器拦截都会在传入时检查请求,然后把(可能被修改过的)请求转发给 `next` 对象的 `handle()` 方法,而 `next` 对象实现了 [`HttpHandler`](api/common/http/HttpHandler) 接口。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Like `intercept()`, the `handle()` method transforms an HTTP request into an `Observable` of [`HttpEvents`](#httpevents) which ultimately include the server's response. The `intercept()` method could inspect that observable and alter it before returning it to the caller.",
+ "translation": "像 `intercept()` 一样,`handle()` 方法也会把 HTTP 请求转换成 [`HttpEvents`](#httpevents) 组成的 `Observable`,它最终包含的是来自服务器的响应。\n`intercept()` 函数可以检查这个可观察对象,并在把它返回给调用者之前修改它。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "This _no-op_ interceptor simply calls `next.handle()` with the original request and returns the observable without doing a thing.",
+ "translation": "这个*无操作的*拦截器,会直接使用原始的请求调用 `next.handle()`,并返回它返回的可观察对象,而不做任何后续处理。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "#### The _next_ object",
+ "translation": "#### `next` 对象",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `next` object represents the next interceptor in the chain of interceptors. \nThe final `next` in the chain is the `HttpClient` backend handler that sends the request to the server and receives the server's response.",
+ "translation": "`next` 对象表示拦截器链表中的下一个拦截器。\n这个链表中的最后一个 `next` 对象就是 `HttpClient` 的后端处理器(backend handler),它会把请求发给服务器,并接收服务器的响应。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Most interceptors call `next.handle()` so that the request flows through to the next interceptor and, eventually, the backend handler.\nAn interceptor _could_ skip calling `next.handle()`, short-circuit the chain, and [return its own `Observable`](#caching) with an artificial server response.",
+ "translation": "大多数的拦截器都会调用 `next.handle()`,以便这个请求流能走到下一个拦截器,并最终传给后端处理器。\n拦截器也*可以*不调用 `next.handle()`,使这个链路短路,并返回一个带有人工构造出来的服务器响应的 [自己的`Observable`](#caching)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "This is a common middleware pattern found in frameworks such as Express.js.",
+ "translation": "这是一种常见的中间件模式,在像 Express.js 这样的框架中也会找到它。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "#### Provide the interceptor",
+ "translation": "#### 提供这个拦截器",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `NoopInterceptor` is a service managed by Angular's [dependency injection (DI)](guide/dependency-injection) system. \nLike other services, you must provide the interceptor class before the app can use it.",
+ "translation": "这个 `NoopInterceptor` 就是一个由 Angular [依赖注入 (DI)](guide/dependency-injection)系统管理的服务。\n像其它服务一样,你也必须先提供这个拦截器类,应用才能使用它。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Because interceptors are (optional) dependencies of the `HttpClient` service, \nyou must provide them in the same injector (or a parent of the injector) that provides `HttpClient`. \nInterceptors provided _after_ DI creates the `HttpClient` are ignored.",
+ "translation": "由于拦截器是 `HttpClient` 服务的(可选)依赖,所以你必须在提供 `HttpClient` 的同一个(或其各级父注入器)注入器中提供这些拦截器。\n那些在 DI 创建完 `HttpClient` *之后*再提供的拦截器将会被忽略。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "This app provides `HttpClient` in the app's root injector, as a side-effect of importing the `HttpClientModule` in `AppModule`.\nYou should provide interceptors in `AppModule` as well.",
+ "translation": "由于在 `AppModule` 中导入了 `HttpClientModule`,导致本应用在其根注入器中提供了 `HttpClient`。所以你也同样要在 `AppModule` 中提供这些拦截器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "After importing the `HTTP_INTERCEPTORS` injection token from `@angular/common/http`,\nwrite the `NoopInterceptor` provider like this:",
+ "translation": "在从 `@angular/common/http` 中导入了 `HTTP_INTERCEPTORS` 注入令牌之后,编写如下的 `NoopInterceptor` 提供商注册语句:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Note the `multi: true` option. \nThis required setting tells Angular that `HTTP_INTERCEPTORS` is a token for a _multiprovider_ \nthat injects an array of values, rather than a single value.",
+ "translation": "注意 `multi: true` 选项。\n这个必须的选项会告诉 Angular `HTTP_INTERCEPTORS` 是一个*多重提供商*的令牌,表示它会注入一个多值的数组,而不是单一的值。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You _could_ add this provider directly to the providers array of the `AppModule`.\nHowever, it's rather verbose and there's a good chance that \nyou'll create more interceptors and provide them in the same way.\nYou must also pay [close attention to the order](#interceptor-order) \nin which you provide these interceptors.",
+ "translation": "你*也可以*直接把这个提供商添加到 `AppModule` 中的提供商数组中,不过那样会非常啰嗦。况且,你将来还会用这种方式创建更多的拦截器并提供它们。\n你还要[特别注意提供这些拦截器的顺序](#interceptor-order)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Consider creating a \"barrel\" file that gathers all the interceptor providers into an `httpInterceptorProviders` array, starting with this first one, the `NoopInterceptor`.",
+ "translation": "认真考虑创建一个封装桶(barrel)文件,用于把所有拦截器都收集起来,一起提供给 `httpInterceptorProviders` 数组,可以先从这个 `NoopInterceptor` 开始。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Then import and add it to the `AppModule` _providers array_ like this:",
+ "translation": "然后导入它,并把它加到 `AppModule` 的 *`providers` 数组*中,就像这样:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "As you create new interceptors, add them to the `httpInterceptorProviders` array and\nyou won't have to revisit the `AppModule`.",
+ "translation": "当你再创建新的拦截器时,就同样把它们添加到 `httpInterceptorProviders` 数组中,而不用再修改 `AppModule`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "There are many more interceptors in the complete sample code.",
+ "translation": "在完整版的范例代码中还有更多的拦截器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "#### Interceptor order",
+ "translation": "#### 拦截器的顺序",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Angular applies interceptors in the order that you provide them.\nIf you provide interceptors _A_, then _B_, then _C_, requests will flow in _A->B->C_ and\nresponses will flow out _C->B->A_.",
+ "translation": "Angular 会按照你提供它们的顺序应用这些拦截器。\n如果你提供拦截器的顺序是先 *A*,再 *B*,再 *C*,那么请求阶段的执行顺序就是 *A->B->C*,而响应阶段的执行顺序则是\n *C->B->A*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You cannot change the order or remove interceptors later.\nIf you need to enable and disable an interceptor dynamically, you'll have to build that capability into the interceptor itself.",
+ "translation": "以后你就再也不能修改这些顺序或移除某些拦截器了。\n如果你需要动态启用或禁用某个拦截器,那就要在那个拦截器中自行实现这个功能。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You may have expected the `intercept()` and `handle()` methods to return observables of `HttpResponse` as most `HttpClient` methods do.",
+ "translation": "你可能会期望 `intercept()` 和 `handle()` 方法会像大多数 `HttpClient` 中的方法那样返回 `HttpResponse` 的可观察对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Instead they return observables of `HttpEvent`.",
+ "translation": "然而并没有,它们返回的是 `HttpEvent` 的可观察对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "That's because interceptors work at a lower level than those `HttpClient` methods. A single HTTP request can generate multiple _events_, including upload and download progress events. The `HttpResponse` class itself is actually an event, whose type is `HttpEventType.HttpResponseEvent`.",
+ "translation": "这是因为拦截器工作的层级比那些 `HttpClient` 方法更低一些。每个 HTTP 请求都可能会生成很多个*事件*,包括上传和下载的进度事件。\n实际上,`HttpResponse` 类本身就是一个事件,它的类型(`type`)是 `HttpEventType.HttpResponseEvent`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Many interceptors are only concerned with the outgoing request and simply return the event stream from `next.handle()` without modifying it.",
+ "translation": "很多拦截器只关心发出的请求,而对 `next.handle()` 返回的事件流不会做任何修改。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "But interceptors that examine and modify the response from `next.handle()` \nwill see all of these events. \nYour interceptor should return _every event untouched_ unless it has a _compelling reason to do otherwise_.",
+ "translation": "但那些要检查和修改来自 `next.handle()` 的响应体的拦截器希望看到所有这些事件。\n所以,你的拦截器应该返回*你没碰过的所有事件*,除非你*有充分的理由不这么做*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "#### Immutability",
+ "translation": "#### 不可变性",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Although interceptors are capable of mutating requests and responses,\nthe `HttpRequest` and `HttpResponse` instance properties are `readonly`,\nrendering them largely immutable.",
+ "translation": "虽然拦截器有能力改变请求和响应,但 `HttpRequest` 和 `HttpResponse` 实例的属性却是只读(`readonly`)的,\n因此,它们在很大意义上说是不可变对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "They are immutable for a good reason: the app may retry a request several times before it succeeds, which means that the interceptor chain may re-process the same request multiple times.\nIf an interceptor could modify the original request object, the re-tried operation would start from the modified request rather than the original. Immutability ensures that interceptors see the same request for each try.",
+ "translation": "有充足的理由把它们做成不可变对象:应用可能会重试发送很多次请求之后才能成功,这就意味着这个拦截器链表可能会多次重复处理同一个请求。\n如果拦截器可以修改原始的请求对象,那么重试阶段的操作就会从修改过的请求开始,而不是原始请求。\n而这种不可变性,可以确保这些拦截器在每次重试时看到的都是同样的原始请求。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "TypeScript will prevent you from setting `HttpRequest` readonly properties.",
+ "translation": "通过把 `HttpRequest` 的属性设置为只读的,TypeScript 可以防止你犯这种错误。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "To alter the request, clone it first and modify the clone before passing it to `next.handle()`. \nYou can clone and modify the request in a single step as in this example.",
+ "translation": "要想修改该请求,就要先克隆它,并修改这个克隆体,然后再把这个克隆体传给 `next.handle()`。\n你可以用一步操作中完成对请求的克隆和修改,例子如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `clone()` method's hash argument allows you to mutate specific properties of the request while copying the others.",
+ "translation": "这个 `clone()` 方法的哈希型参数允许你在复制出克隆体的同时改变该请求的某些特定属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "##### The request body",
+ "translation": "##### 请求体",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `readonly` assignment guard can't prevent deep updates and, in particular, \nit can't prevent you from modifying a property of a request body object.",
+ "translation": "`readonly` 这种赋值保护,无法防范深修改(修改子对象的属性),也不能防范你修改请求体对象中的属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If you must mutate the request body, copy it first, change the copy, \n`clone()` the request, and set the clone's body with the new body, as in the following example.",
+ "translation": "如果你必须修改请求体,那么就要先复制它,然后修改这个复本,`clone()` 这个请求,然后把这个请求体的复本作为新的请求体,例子如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "##### Clearing the request body",
+ "translation": "##### 清空请求体",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Sometimes you need to clear the request body rather than replace it.\nIf you set the cloned request body to `undefined`, Angular assumes you intend to leave the body as is.\nThat is not what you want.\nIf you set the cloned request body to `null`, Angular knows you intend to clear the request body.",
+ "translation": "有时你需要清空请求体,而不是替换它。\n如果你把克隆后的请求体设置成 `undefined`,Angular 会认为你是想让这个请求体保持原样。\n这显然不是你想要的。\n但如果把克隆后的请求体设置成 `null`,那 Angular 就知道你是想清空这个请求体了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "#### Set default headers",
+ "translation": "#### 设置默认请求头",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Apps often use an interceptor to set default headers on outgoing requests.",
+ "translation": "应用通常会使用拦截器来设置外发请求的默认请求头。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The sample app has an `AuthService` that produces an authorization token.\nHere is its `AuthInterceptor` that injects that service to get the token and\nadds an authorization header with that token to every outgoing request:",
+ "translation": "该范例应用具有一个 `AuthService`,它会生成一个认证令牌。\n在这里,`AuthInterceptor` 会注入该服务以获取令牌,并对每一个外发的请求添加一个带有该令牌的认证头:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The practice of cloning a request to set new headers is so common that \nthere's a `setHeaders` shortcut for it:",
+ "translation": "这种在克隆请求的同时设置新请求头的操作太常见了,因此它还有一个快捷方式 `setHeaders`:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "An interceptor that alters headers can be used for a number of different operations, including:",
"translation": "这种可以修改头的拦截器可以用于很多不同的操作,比如:",
@@ -10059,16 +11614,171 @@
"translation": "#### 记日志",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Because interceptors can process the request and response _together_, they can do things like time and log \nan entire HTTP operation.",
+ "translation": "因为拦截器可以*同时*处理请求和响应,所以它们也可以对整个 HTTP 操作进行计时和记录日志。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Consider the following `LoggingInterceptor`, which captures the time of the request,\nthe time of the response, and logs the outcome with the elapsed time\nwith the injected `MessageService`.",
+ "translation": "考虑下面这个 `LoggingInterceptor`,它捕获请求的发起时间、响应的接收时间,并使用注入的 `MessageService` 来发送总共花费的时间。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The RxJS `tap` operator captures whether the request succeed or failed.\nThe RxJS `finalize` operator is called when the response observable either errors or completes (which it must),\nand reports the outcome to the `MessageService`.",
+ "translation": "RxJS 的 `tap` 操作符会捕获请求成功了还是失败了。\nRxJS 的 `finalize` 操作符无论在响应成功还是失败时都会调用(这是必须的),然后把结果汇报给 `MessageService`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Neither `tap` nor `finalize` touch the values of the observable stream returned to the caller.",
+ "translation": "在这个可观察对象的流中,无论是 `tap` 还是 `finalize` 接触过的值,都会照常发送给调用者。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "#### Caching",
"translation": "#### 缓存",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Interceptors can handle requests by themselves, without forwarding to `next.handle()`.",
+ "translation": "拦截器还可以自行处理这些请求,而不用转发给 `next.handle()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "For example, you might decide to cache certain requests and responses to improve performance.\nYou can delegate caching to an interceptor without disturbing your existing data services.",
+ "translation": "比如,你可能会想缓存某些请求和响应,以便提升性能。\n你可以把这种缓存操作委托给某个拦截器,而不破坏你现有的各个数据服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `CachingInterceptor` demonstrates this approach.",
+ "translation": "`CachingInterceptor` 演示了这种方式。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `isCachable()` function determines if the request is cachable.\nIn this sample, only GET requests to the npm package search api are cachable.",
+ "translation": "`isCachable()` 函数用于决定该请求是否允许缓存。\n在这个例子中,只有发到 npm 包搜索 API 的 GET 请求才是可以缓存的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If the request is not cachable, the interceptor simply forwards the request \nto the next handler in the chain.",
+ "translation": "如果该请求是不可缓存的,该拦截器只会把该请求转发给链表中的下一个处理器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If a cachable request is found in the cache, the interceptor returns an `of()` _observable_ with\nthe cached response, by-passing the `next` handler (and all other interceptors downstream).",
+ "translation": "如果可缓存的请求在缓存中找到了,该拦截器就会通过 `of()` 函数返回一个已缓存的响应体的*可观察对象*,然后把它传给 `next` 处理器(以及所有其它下游拦截器)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If a cachable request is not in cache, the code calls `sendRequest`.",
+ "translation": "如果可缓存的请求在缓存中没找到,代码就会调用 `sendRequest`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `sendRequest` function creates a [request clone](#immutability) without headers\nbecause the npm api forbids them.",
+ "translation": "`sendRequest` 函数创建了一个不带请求头的[请求克隆体](#immutability),因为 npm API 不会接受它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "It forwards that request to `next.handle()` which ultimately calls the server and\nreturns the server's response.",
+ "translation": "它会把这个请求转发给 `next.handle()`,它最终会调用服务器,并且返回服务器的响应。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Note how `sendRequest` _intercepts the response_ on its way back to the application.\nIt _pipes_ the response through the `tap()` operator,\nwhose callback adds the response to the cache.",
+ "translation": "注意 `sendRequest` 是如何在发回给应用之前*拦截这个响应的*。\n它会通过 `tap()` 操作符对响应进行管道处理,并在其回调中把响应加到缓存中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The original response continues untouched back up through the chain of interceptors\nto the application caller.",
+ "translation": "然后,原始的响应会通过这些拦截器链,原封不动的回到服务器的调用者那里。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Data services, such as `PackageSearchService`, are unaware that \nsome of their `HttpClient` requests actually return cached responses.",
+ "translation": "数据服务,比如 `PackageSearchService`,并不知道它们收到的某些 `HttpClient` 请求实际上是从缓存的请求中返回来的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "#### Return a multi-valued _Observable_",
+ "translation": "#### 返回多值可观察对象",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `HttpClient.get()` method normally returns an _observable_ \nthat either emits the data or an error. \nSome folks describe it as a \"_one and done_\" observable.",
+ "translation": "`HttpClient.get()` 方法正常情况下只会返回一个*可观察对象*,它或者发出数据,或者发出错误。\n有些人说它是“一次性完成”的可观察对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "But an interceptor can change this to an _observable_ that emits more than once.",
+ "translation": "但是拦截器也可以把这个修改成发出多个值的*可观察对象*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "A revised version of the `CachingInterceptor` optionally returns an _observable_ that\nimmediately emits the cached response, sends the request to the npm web api anyway,\nand emits again later with the updated search results.",
+ "translation": "修改后的 `CachingInterceptor` 版本可以返回一个立即发出缓存的响应,然后仍然把请求发送到 npm 的 Web API,然后再把修改过的搜索结果重新发出一次。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The _cache-then-refresh_ option is triggered by the presence of a **custom `x-refresh` header**.",
+ "translation": "这种*缓存并刷新*的选项是由**自定义的 `x-refresh` 头**触发的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "A checkbox on the `PackageSearchComponent` toggles a `withRefresh` flag,\nwhich is one of the arguments to `PackageSearchService.search()`.\nThat `search()` method creates the custom `x-refresh` header\nand adds it to the request before calling `HttpClient.get()`.",
+ "translation": "`PackageSearchComponent` 中的一个检查框会切换 `withRefresh` 标识,\n它是 `PackageSearchService.search()` 的参数之一。\n`search()` 方法创建了自定义的 `x-refresh` 头,并在调用 `HttpClient.get()` 前把它添加到请求里。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The revised `CachingInterceptor` sets up a server request \nwhether there's a cached value or not, \nusing the same `sendRequest()` method described [above](#send-request).\nThe `results$` observable will make the request when subscribed.",
+ "translation": "修改后的 `CachingInterceptor` 会发起一个服务器请求,而不管有没有缓存的值。\n就像 [前面](#send-request) 的 `sendRequest()` 方法一样进行订阅。\n在订阅 `results$` 可观察对象时,就会发起这个请求。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If there's no cached value, the interceptor returns `results$`.",
+ "translation": "如果没有缓存的值,拦截器直接返回 `result$`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If there is a cached value, the code _pipes_ the cached response onto\n`results$`, producing a recomposed observable that emits twice,\nthe cached response first (and immediately), followed later\nby the response from the server.\nSubscribers see a sequence of _two_ responses.",
+ "translation": "如果有缓存的值,这些代码就会把缓存的响应加入到 `result$` 的管道中,使用重组后的可观察对象进行处理,并发出两次。\n先立即发出一次缓存的响应体,然后发出来自服务器的响应。\n订阅者将会看到一个包含这*两个*响应的序列。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "### Listening to progress events",
"translation": "### 监听进度事件",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Sometimes applications transfer large amounts of data and those transfers can take a long time.\nFile uploads are a typical example. \nGive the users a better experience by providing feedback on the progress of such transfers.",
+ "translation": "有事,应用会传输大量数据,并且这些传输可能会花费很长时间。\n典型的例子是文件上传。\n可以通过在传输过程中提供进度反馈,来提升用户体验。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "To make a request with progress events enabled, you can create an instance of `HttpRequest` \nwith the `reportProgress` option set true to enable tracking of progress events.",
+ "translation": "要想开启进度事件的响应,你可以创建一个把 `reportProgress` 选项设置为 `true` 的 `HttpRequest` 实例,以开启进度跟踪事件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Every progress event triggers change detection, so only turn them on if you truly intend to report progress in the UI.",
+ "translation": "每个进度事件都会触发变更检测,所以,你应该只有当确实希望在 UI 中报告进度时才打开这个选项。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Next, pass this request object to the `HttpClient.request()` method, which\nreturns an `Observable` of `HttpEvents`, the same events processed by interceptors:",
+ "translation": "接下来,把这个请求对象传给 `HttpClient.request()` 方法,它返回一个 `HttpEvents` 的 `Observable`,同样也可以在拦截器中处理这些事件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The `getEventMessage` method interprets each type of `HttpEvent` in the event stream.",
+ "translation": "`getEventMessage` 方法会解释事件流中的每一个 `HttpEvent` 类型。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The sample app for this guide doesn't have a server that accepts uploaded files.\nThe `UploadInterceptor` in `app/http-interceptors/upload-interceptor.ts` \nintercepts and short-circuits upload requests\nby returning an observable of simulated events.",
+ "translation": "这个范例应用中并没有一个用来接收上传的文件的真实的服务器。\n`app/http-interceptors/upload-interceptor.ts` 中的 `UploadInterceptor` 会拦截并短路掉上传请求,改为返回一个带有各个模拟事件的可观察对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "## Security: XSRF Protection",
"translation": "## 安全:XSRF 防护",
@@ -10124,6 +11834,31 @@
"translation": "### Mock 方法论",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Angular's HTTP testing library is designed for a pattern of testing wherein \nthe the app executes code and makes requests first.",
+ "translation": "Angular 的 HTTP 测试库是专为其中的测试模式而设计的。在这种模式下,会首先在应用中执行代码并发起请求。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Then a test expects that certain requests have or have not been made, \nperforms assertions against those requests, \nand finally provide responses by \"flushing\" each expected request.",
+ "translation": "然后,每个测试会期待发起或未发起过某个请求,对这些请求进行断言,\n最终对每个所预期的请求进行刷新(flush)来对这些请求提供响应。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "At the end, tests may verify that the app has made no unexpected requests.",
+ "translation": "最终,测试可能会验证这个应用不曾发起过非预期的请求。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You can run these sample tests \nin a live coding environment.",
+ "translation": "你可以到在线编程环境中运行这些范例测试 。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "The tests described in this guide are in `src/testing/http-client.spec.ts`.\nThere are also tests of an application data service that call `HttpClient` in\n`src/app/heroes/heroes.service.spec.ts`.",
+ "translation": "本章所讲的这些测试位于 `src/testing/http-client.spec.ts` 中。\n在 `src/app/heroes/heroes.service.spec.ts` 中还有一些测试,用于测试那些调用了 `HttpClient` 的数据服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "### Setup",
"translation": "### 环境设置",
@@ -10134,11 +11869,31 @@
"translation": "要开始测试那些通过`HttpClient`发起的请求,就要导入`HttpClientTestingModule`模块,并把它加到你的`TestBed` 设置里去,代码如下:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Then add the `HttpClientTestingModule` to the `TestBed` and continue with\nthe setup of the _service-under-test_.",
+ "translation": "然后把 `HTTPClientTestingModule` 添加到 `TestBed` 中,并继续设置*被测服务*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Now requests made in the course of your tests will hit the testing backend instead of the normal backend.",
+ "translation": "现在,在测试中发起的这些请求将会被这些测试后端(testing backend)处理,而不是标准的后端。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "This setup also calls `TestBed.get()` to inject the `HttpClient` service and the mocking controller\nso they can be referenced during the tests.",
+ "translation": "这种设置还会调用 `TestBed.get()`,来获取注入的 `HttpClient` 服务和模拟对象的控制器 `HttpTestingController`,以便在测试期间引用它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "### Expecting and answering requests",
"translation": "### 期待并回复请求",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "Now you can write a test that expects a GET Request to occur and provides a mock response.",
+ "translation": "现在,你就可以编写测试,等待 GET 请求并给出模拟响应。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "The last step, verifying that no requests remain outstanding, is common enough for you to move it into an `afterEach()` step:",
"translation": "最后一步,验证没有发起过预期之外的请求,足够通用,因此我们可以把它移到`afterEach()`中:",
@@ -10146,7 +11901,12 @@
},
{
"original": "#### Custom request expectations",
- "translation": "#### 自定义请求的预期",
+ "translation": "#### 自定义对请求的预期",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "If matching by URL isn't sufficient, it's possible to implement your own matching function. \nFor example, you could look for an outgoing request that has an authorization header:",
+ "translation": "如果仅根据 URL 匹配还不够,你还可以自行实现匹配函数。\n比如,你可以验证外发的请求是否带有某个认证头:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
{
@@ -10164,6 +11924,21 @@
"translation": "如果我们需要在测试中对重复的请求进行响应,可以使用`match()` API 来代替 `expectOne()`,它的参数不变,但会返回一个与这些请求相匹配的数组。一旦返回,这些请求就会从将来要匹配的列表中移除,我们要自己验证和刷新(flush)它。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
},
+ {
+ "original": "### Testing for errors",
+ "translation": "### 测试对错误的预期",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "You should test the app's defenses against HTTP requests that fail.",
+ "translation": "你还要测试应用对于 HTTP 请求失败时的防护。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
+ {
+ "original": "Call `request.error()` with an `ErrorEvent` instead of `request.flush()`, as in this example.",
+ "translation": "那就调用 `request.error()`,并给它传入一个 `ErrorEvent`,而不是 `request.flush()`。例子如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/http.md"
+ },
{
"original": "# Internationalization (i18n)",
"translation": "# 国际化(i18n)",
@@ -13124,6 +14899,11 @@
"translation": "Angular通过*变更检测*过程来查找绑定值的更改,并在每一次JavaScript事件之后运行:每次按键、鼠标移动、定时器以及服务器的响应。\n这可能会让变更检测显得很昂贵,但是Angular会尽可能降低变更检测的成本。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/pipes.md"
},
+ {
+ "original": "Angular picks a simpler, faster change detection algorithm when you use a pipe.",
+ "translation": "当使用管道时,Angular 会选用一种更简单、更快速的变更检测算法。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/pipes.md"
+ },
{
"original": "No pipe ",
"translation": "无管道 ",
@@ -15524,6 +17304,11 @@
"translation": "RouterLink
(路由链接)",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
+ {
+ "original": "The directive for binding a clickable HTML element to\n a route. Clicking an element with a routerLink
directive\n that is bound to a string or a link parameters array triggers a navigation.",
+ "translation": "这个指令把可点击的 HTML 元素绑定到某个路由。点击带有 routerLink
指令(绑定到字符串 或链接参数数组 )的元素时就会触发一次导航。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
+ },
{
"original": "RouterLinkActive
",
"translation": "RouterLinkActive
(活动路由链接)",
@@ -15676,7 +17461,7 @@
},
{
"original": "Try it by clicking on this live example link .",
- "translation": "点击 试用一下。",
+ "translation": "点击 试用一下。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
{
@@ -16286,7 +18071,7 @@
},
{
"original": "This example recreates the heroes feature in the \"Services\" episode of the\n[Tour of Heroes tutorial](tutorial/toh-pt4 \"Tour of Heroes: Services\"),\nand you'll be copying much of the code\nfrom the .",
- "translation": "这个例子重写了[《英雄指南》](tutorial/toh-pt4 \"Tour of Heroes: Services\")的“服务”部分的英雄列表特性,我们可以从 中赋值大部分代码过来。",
+ "translation": "这个例子重写了[《英雄指南》](tutorial/toh-pt4 \"Tour of Heroes: Services\")的“服务”部分的英雄列表特性,我们可以从 中赋值大部分代码过来。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
{
@@ -16336,7 +18121,7 @@
},
{
"original": "* Copy into it the contents of the `app.component.ts` from\n the \"Services\" tutorial .",
- "translation": "把教程中的“服务”部分 的代码复制到`app.component.ts`中。",
+ "translation": "把 复制到`app.component.ts`中。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
{
@@ -16634,6 +18419,16 @@
"translation": "然后,在`ngOnInit`方法中,我们用`ActivatedRoute`服务来接收路由的参数,从参数中取得该英雄的`id`,并接收此英雄用于显示。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
+ {
+ "original": "The `paramMap` processing is a bit tricky. When the map changes, you `get()`\nthe `id` parameter from the changed parameters.",
+ "translation": "`paramMap` 的处理过程有点稍复杂。当这个 map 的值变化时,你可以从变化之后的参数中 `get()` 到其 `id` 参数。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
+ },
+ {
+ "original": "Then you tell the `HeroService` to fetch the hero with that `id` and return the result of the `HeroService` request.",
+ "translation": "然后,让 `HeroService` 去获取一个具有此 `id` 的英雄,并返回这个 `HeroService` 请求的结果。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
+ },
{
"original": "You might think to use the RxJS `map` operator.\nBut the `HeroService` returns an `Observable`.\nSo you flatten the `Observable` with the `switchMap` operator instead.",
"translation": "由于参数是作为`Observable`提供的,所以我们得用`switchMap`操作符来根据名字取得`id`参数,并告诉`HeroService`来获取带有那个`id`的英雄。",
@@ -16641,7 +18436,7 @@
},
{
"original": "The `switchMap` operator also cancels previous in-flight requests. If the user re-navigates to this route\nwith a new `id` while the `HeroService` is still retrieving the old `id`, `switchMap` discards that old request and returns the hero for the new `id`.",
- "translation": "`switchMap`操作符也会取消以前未完成的在途请求。如果用户使用心得`id`再次导航到该路由,而`HeroService`仍在接受老`id`对应的英雄,那么`switchMap`就会抛弃老的请求,并返回这个新`id`的英雄信息。",
+ "translation": "`switchMap`操作符也会取消以前未完成的在途请求。如果用户使用新的`id`再次导航到该路由,而`HeroService`仍在接受老`id`对应的英雄,那么`switchMap`就会抛弃老的请求,并返回这个新`id`的英雄信息。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
{
@@ -16649,11 +18444,6 @@
"translation": "这个可观察对象的`Subscription`(订阅)将会由`AsyncPipe`处理,并且组件的`hero`属性将会设置为刚刚接收到的这个英雄。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
- {
- "original": "#### _ParamMap_ API",
- "translation": "#### _ParamMap_ 参数 API",
- "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
- },
{
"original": "The `ParamMap` API is inspired by the [URLSearchParams interface](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams). It provides methods\nto handle parameter access for both route parameters (`paramMap`) and query parameters (`queryParamMap`).",
"translation": "`ParamMap` API 是参照[URLSearchParams 接口](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)来设计的。它提供了一些方法来处理对路由参数(`paramMap`)和查询参数(`queryParamMap`)中的参数访问。",
@@ -17724,6 +19514,11 @@
"translation": "因此,路由的守卫可以返回一个`Observable`或`Promise`,并且路由器会等待这个可观察对象被解析为`true`或`false`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
+ {
+ "original": "The router supports multiple guard interfaces:",
+ "translation": "路由器可以支持多种守卫接口:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
+ },
{
"original": "* [`CanActivate`](api/router/CanActivate) to mediate navigation *to* a route.",
"translation": "用[`CanActivate`](api/router/CanActivate)来处理导航*到*某路由的情况。",
@@ -17941,12 +19736,12 @@
},
{
"original": "### _CanActivateChild_: guarding child routes",
- "translation": "### `CanAcitvateChild`:保护子路由",
+ "translation": "### `CanActivateChild`:保护子路由",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
{
"original": "You can also protect child routes with the `CanActivateChild` guard.\nThe `CanActivateChild` guard is similar to the `CanActivate` guard.\nThe key difference is that it runs _before_ any child route is activated.",
- "translation": "我们还可以使用`CanActivateChild`守卫来保护子路由。\n`CanActivateChild`守卫和`CanAcitvate`守卫很像。\n它们的区别在于,`CanActivateChild`会在*任何子路由*被激活之前运行。",
+ "translation": "我们还可以使用`CanActivateChild`守卫来保护子路由。\n`CanActivateChild`守卫和`CanActivate`守卫很像。\n它们的区别在于,`CanActivateChild`会在*任何子路由*被激活之前运行。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
{
@@ -17961,7 +19756,7 @@
},
{
"original": "Next, implement the `canActivateChild()` method which takes the same arguments as the `canActivate()` method:\nan `ActivatedRouteSnapshot` and `RouterStateSnapshot`.\nThe `canActivateChild()` method can return an `Observable` or `Promise` for\nasync checks and a `boolean` for sync checks.\nThis one returns a `boolean`:",
- "translation": "接下来,实现`CanAcitvateChild`方法,它所接收的参数与`CanAcitvate`方法一样:一个`ActivatedRouteSnapshot`和一个`RouterStateSnapshot`。\n`CanAcitvateChild`方法可以返回`Observable`或`Promise`来支持异步检查,或`boolean`来支持同步检查。\n这里返回的是`boolean`:",
+ "translation": "接下来,实现`CanActivateChild`方法,它所接收的参数与`CanActivate`方法一样:一个`ActivatedRouteSnapshot`和一个`RouterStateSnapshot`。\n`CanActivateChild`方法可以返回`Observable`或`Promise`来支持异步检查,或`boolean`来支持同步检查。\n这里返回的是`boolean`:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
{
@@ -18234,6 +20029,11 @@
"translation": "还可以再导航之间**保留**查询参数和片段,而无需再次再导航中提供。在`LoginComponent`中的`router.navigate`方法中,添加第二个参数,该**对象**提供了`preserveQueryParams`和 `preserveFragment`,用于传递到当前的查询参数中并为下一个路由提供片段。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
+ {
+ "original": "The `queryParamsHandling` feature also provides a `merge` option, which will preserve and combine the current query parameters with any provided query parameters\nwhen navigating.",
+ "translation": "`queryParamsHandling` 特性还提供了 `merge` 选项,它将会在导航时保留当前的查询参数,并与其它查询参数合并。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
+ },
{
"original": "Since you'll be navigating to the *Admin Dashboard* route after logging in, you'll update it to handle the\nquery parameters and fragment.",
"translation": "由于要在登录后导航到*危机管理*特征区的路由,所以我们还得更新它,来处理这些全局查询参数和片段。",
@@ -18356,7 +20156,7 @@
},
{
"original": "You're already protecting the `AdminModule` with a `CanActivate` guard that prevents unauthorized users from\naccessing the admin feature area.\nIt redirects to the login page if the user is not authorized.",
- "translation": "我们已经使用`CanAcitvate`保护`AdminModule`了,它会阻止未授权用户访问管理特性区。如果用户未登录,它就会跳转到登录页。",
+ "translation": "我们已经使用`CanActivate`保护`AdminModule`了,它会阻止未授权用户访问管理特性区。如果用户未登录,它就会跳转到登录页。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/router.md"
},
{
@@ -19814,6 +21614,21 @@
"translation": "对于现实项目开发,我们强烈推荐在[本地开发](guide/setup#develop-locally)。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/setup.md"
},
+ {
+ "original": "## Appendix: develop locally with IE",
+ "translation": "## 附录:使用 IE 进行本地开发",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/setup.md"
+ },
+ {
+ "original": "If you develop angular locally with `ng serve`, there will be `websocket` connection being setup automatically between browser and local dev server, so when your code change, browser can automatically refresh.",
+ "translation": "如果你使用 `ng serve` 进行本地开发,就会在浏览器和本地开发服务器之间自动建立一个 `WebSocket` 连接,因此,当你的代码变化时,浏览器也会自动刷新。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/setup.md"
+ },
+ {
+ "original": "In windows, by default one application can only have 6 websocket connections, MSDN WebSocket Settings .\nSo if IE was refreshed manunally or automatically by `ng serve`, sometimes, the websocket will not close properly, when websocket connections exceed limitations, `SecurityError` will be thrown, this error will not affect the angular application, you can just restart IE to clear this error, or modify the windows registry to update the limitations.",
+ "translation": "在 Windows 中,默认情况下一个应用只能有六个 WebSocket 连接,参见 MSDN 中的 WebSocket 设置部分 。\n所以,如果 IE 手动刷新或被 `ng serve` 触发了自动刷新,有时候 WebSocket 可能无法正常关闭,当 WebSocket 的连接数超限时,就会抛出 `SecurityError` 异常。请放心,这个异常对 Angular 应用没什么影响,你重启一下 IE 就能消除这个错误,或者修改 Windows 注册表来修改这个上限。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/setup.md"
+ },
{
"original": "Note the following:",
"translation": "请注意以下几点:",
@@ -20471,7 +22286,7 @@
},
{
"original": "Nobody reads the `appUnless` property so it doesn't need a getter.",
- "translation": "没有人会读取`appUnless`属性,因此它不需要定义设置器(getter)。",
+ "translation": "没有人会读取`appUnless`属性,因此它不需要定义 getter。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/structural-directives.md"
},
{
@@ -21399,6 +23214,16 @@
"translation": "**考虑**不要在接口名字前面加`I`前缀。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/styleguide.md"
},
+ {
+ "original": "**Consider** using a class instead of an interface for services and declarables (components, directives, and pipes).",
+ "translation": "**考虑**在服务和可声明对象(组件、指令和管道)中用类代替接口。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/styleguide.md"
+ },
+ {
+ "original": "**Consider** using an interface for data models.",
+ "translation": "**考虑**用接口作为数据模型。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/styleguide.md"
+ },
{
"original": "**Why?** TypeScript guidelines \ndiscourage the `I` prefix.",
"translation": "**为何?**TypeScript 指导原则 不建议使用 “I” 前缀。",
@@ -21416,7 +23241,7 @@
},
{
"original": "**Why?** An interface-class can be a provider lookup token in Angular dependency injection.",
- "translation": "**为何?**在 Angular 依赖注入系统中,接口类可以作为服务提供商的查找令牌。",
+ "translation": "**为何?**在 Angular 依赖注入系统中,接口类(译注:指写成类的形式,但是只当做接口使用)可以作为服务提供商的查找令牌。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/styleguide.md"
},
{
@@ -22204,6 +24029,11 @@
"translation": "**为何?**查看组件模板的 HTML 时,更容易识别一个符号是组件还是指令。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/styleguide.md"
},
+ {
+ "original": "There are a few cases where you give a component an attribute, such as when you want to augment a built-in element. For example, [Material Design](https://material.angular.io/components/button/overview) uses this technique with ``. However, you wouldn't use this technique on a custom element.",
+ "translation": "少数情况下,你要为组件使用属性选择器,比如你要加强某个内置元素时。\n比如,[Material Design 组件库](https://material.angular.io/components/button/overview)就会对 `` 使用这项技术。不过,你不应该在自定义组件上使用这项技术。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/styleguide.md"
+ },
{
"original": "Back to top ",
"translation": "回到顶部 ",
@@ -23396,32 +25226,32 @@
},
{
"original": "For example, when the browser renders ` `, it creates a\ncorresponding DOM node with a `value` property *initialized* to \"Bob\".",
- "translation": "例如,当浏览器渲染` `时,它将创建相应 DOM 节点,\n其`value` property 被*初始化为* “Bob”。",
+ "translation": "例如,当浏览器渲染` `时,它将创建相应 DOM 节点,\n它的 `value` 这个 property 被*初始化为* “Bob”。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
{
"original": "When the user enters \"Sally\" into the input box, the DOM element `value` *property* becomes \"Sally\".\nBut the HTML `value` *attribute* remains unchanged as you discover if you ask the input element\nabout that attribute: `input.getAttribute('value')` returns \"Bob\".",
- "translation": "当用户在输入框中输入 “Sally” 时,DOM 元素的`value` *property* 变成了 “Sally”。\n但是这个 HTML `value` *attribute* 保持不变。如果我们读取 input 元素的 attribute,就会发现确实没变:\n`input.getAttribute('value') // 返回 \"Bob\"`。",
+ "translation": "当用户在输入框中输入 “Sally” 时,DOM 元素的 `value` 这个 *property* 变成了 “Sally”。\n但是该 HTML 的 `value` 这个 *attribute* 保持不变。如果我们读取 input 元素的 attribute,就会发现确实没变:\n`input.getAttribute('value') // 返回 \"Bob\"`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
{
"original": "The HTML attribute `value` specifies the *initial* value; the DOM `value` property is the *current* value.",
- "translation": "HTML attribute `value`指定了*初始*值;DOM `value` property 是*当前*值。",
+ "translation": "HTML 的 `value` 这个 attribute 指定了*初始*值;DOM 的 `value` 这个 property 是*当前*值。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
{
"original": "The `disabled` attribute is another peculiar example. A button's `disabled` *property* is\n`false` by default so the button is enabled.\nWhen you add the `disabled` *attribute*, its presence alone initializes the button's `disabled` *property* to `true`\nso the button is disabled.",
- "translation": "`disabled` attribute 是另一个古怪的例子。按钮的`disabled` *property* 是`false`,因为默认情况下按钮是可用的。\n当我们添加`disabled` *attribute* 时,只要它出现了按钮的`disabled` *property* 就初始化为`true`,于是按钮就被禁用了。",
+ "translation": "`disabled` 这个 attribute 是另一种特例。按钮的`disabled` 这个 *property* 是`false`,因为默认情况下按钮是可用的。\n当我们添加`disabled` 这个 *attribute* 时,只要它出现了按钮的`disabled` 这个 *property* 就初始化为`true`,于是按钮就被禁用了。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
{
"original": "Adding and removing the `disabled` *attribute* disables and enables the button. The value of the *attribute* is irrelevant,\nwhich is why you cannot enable a button by writing `Still Disabled `.",
- "translation": "添加或删除`disabled` *attribute*会禁用或启用这个按钮。但 *attribute* 的值无关紧要,这就是我们为什么没法通过\n`仍被禁用 `这种写法来启用按钮。",
+ "translation": "添加或删除`disabled` 这个 *attribute*会禁用或启用这个按钮。但 *attribute* 的值无关紧要,这就是我们为什么没法通过\n`仍被禁用 `这种写法来启用按钮。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
{
"original": "Setting the button's `disabled` *property* (say, with an Angular binding) disables or enables the button.\nThe value of the *property* matters.",
- "translation": "设置按钮的`disabled` *property*(如,通过 Angular 绑定)可以禁用或启用这个按钮。\n这就是 *property* 的价值。",
+ "translation": "设置按钮的`disabled` 这个 *property*(如,通过 Angular 绑定)可以禁用或启用这个按钮。\n这就是 *property* 的价值。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
{
@@ -23824,21 +25654,6 @@
"translation": "这里是表格渲染出来的样子:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
- {
- "original": "One-Two",
- "translation": "1-2列",
- "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
- },
- {
- "original": "Five",
- "translation": "五列",
- "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
- },
- {
- "original": "Six",
- "translation": "六列",
- "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
- },
{
"original": "One of the primary use cases for attribute binding\nis to set ARIA attributes, as in this example:",
"translation": "attribute 绑定的主要用例之一是设置 ARIA attribute(译注:ARIA指可访问性,用于给残障人士访问互联网提供便利),\n就像这个例子中一样:",
@@ -24391,7 +26206,7 @@
},
{
"original": "* to use [``](guide/structural-directives#ngcontainer \"\")\nto group elements when there is no suitable host element for the directive.",
- "translation": "当没有合适的宿主元素防止指令时,可用``](structural-directives.html#ngcontainer \"对元素进行分组。",
+ "translation": "当没有合适的宿主元素放置指令时,可用 [``](guide/structural-directives#ngcontainer \"\") 对元素进行分组。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
{
@@ -24794,6 +26609,101 @@
"translation": "在下面的例子中,`iconUrl`和`onSave`是组件的成员,它们在`=`右侧引号语法中被引用了。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
+ {
+ "original": "The `iconUrl` and `onSave` are members of the `AppComponent` class.\nThey are _not_ decorated with `@Input()` or `@Output`.\nAngular does not object.",
+ "translation": "`iconUrl` 和 `onSave` 是 `AppComponent` 类的成员。但它们并没有带 `@Input()` 或 `@Output()` 装饰器。\nAngular 不在乎。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "**You can always bind to a public property of a component in its own template.**\nIt doesn't have to be an _Input_ or _Output_ property",
+ "translation": "**你总是可以在组件自己的模板中绑定到组件的公共属性,**而不用管它们是否输入(Input)属性或输出(Output)属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "A component's class and template are closely coupled.\nThey are both parts of the same thing.\nTogether they _are_ the component.\nExchanges between a component class and its template are internal implementation details.",
+ "translation": "这是因为组件类和模板是紧耦合的,它们是同一个东西的两个部分,合起来构成组件。\n组件类及其模板之间的交互属于实现细节。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "### Binding to a different component",
+ "translation": "### 绑定到其它组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "You can also bind to a property of a _different_ component.\nIn such bindings, the _other_ component's property is to the _left_ of the (`=`).",
+ "translation": "你也可以绑定到*其它*组件的属性。\n这种绑定形式下,*其它*组件的属性位于等号(`=`)的*左侧*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "In the following example, the `AppComponent` template binds `AppComponent` class members to properties of the `HeroDetailComponent` whose selector is `'app-hero-detail'`.",
+ "translation": "下面的例子中,`AppComponent` 的模板把 `AppComponent` 类的成员绑定到了 `HeroDetailComponent`(选择器为`'app-hero-detail'`) 的属性上。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "The Angular compiler _may_ reject these bindings with errors like this one:",
+ "translation": "Angular 的编译器*可能*会对这些绑定报错,就像这样:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "You know that `HeroDetailComponent` has `hero` and `deleteRequest` properties.\nBut the Angular compiler refuses to recognize them.",
+ "translation": "你自己知道 `HeroDetailComponent` 有两个属性 `hero` 和 `detectRequest`,但 Angular 编译器并不知道。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "**The Angular compiler won't bind to properties of a different component\nunless they are Input or Output properties**.",
+ "translation": "**Angular 编译器不会绑定到其它组件的属性上 —— 除非这些属性是输入或输出属性。**",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "There's a good reason for this rule.",
+ "translation": "这条规则是有充分理由的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "It's OK for a component to bind to its _own_ properties.\nThe component author is in complete control of those bindings.",
+ "translation": "组件绑定到它*自己*的属性当然没问题。\n该组件的作者对这些绑定有完全的控制权。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "But other components shouldn't have that kind of unrestricted access.\nYou'd have a hard time supporting your component if anyone could bind to any of its properties.\nOutside components should only be able to bind to the component's public binding API.",
+ "translation": "但是,其它组件不应该进行这种毫无限制的访问。\n如果任何人都可以绑定到你的组件的任何属性上,那么这个组件就很难维护。\n所以,外部组件应该只能绑定到组件的公共(允许绑定) API 上。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "Angular asks you to be _explicit_ about that API.\nIt's up to _you_ to decide which properties are available for binding by\nexternal components.",
+ "translation": "Angular 要求你*显式声明*那些 API。\n它让*你*可以自己决定哪些属性是可以被外部组件绑定的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "#### TypeScript _public_ doesn't matter",
+ "translation": "#### TypeScript 的 `public` 是没用的",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "You can't use the TypeScript _public_ and _private_ access modifiers to\nshape the component's public binding API.",
+ "translation": "你不能用 TypeScript 的 `public` 和 `private` 访问控制符来标明组件的公共 API。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "All data bound properties must be TypeScript _public_ properties.\nAngular never binds to a TypeScript _private_ property.",
+ "translation": "所有数据绑定属性必须是 TypeScript 的公共属性,Angular 永远不会绑定到 TypeScript 中的私有属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "Angular requires some other way to identify properties that _outside_ components are allowed to bind to.\nThat _other way_ is the `@Input()` and `@Output()` decorators.",
+ "translation": "因此,Angular 需要一些其它方式来标记出那些允许被*外部*组件绑定到的属性。\n这种*其它方式*,就是 `@Input()` 和 `@Output()` 装饰器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "### Declaring Input and Output properties",
+ "translation": "### 声明输入与输出属性",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "In the sample for this guide, the bindings to `HeroDetailComponent` do not fail\nbecause the data bound properties are annotated with `@Input()` and `@Output()` decorators.",
+ "translation": "在本章的例子中,绑定到 `HeroDetailComponent` 不会失败,这是因为这些要进行数据绑定的属性都带有 `@Input()` 和 `@Output()` 装饰器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
{
"original": "Alternatively, you can identify members in the `inputs` and `outputs` arrays\nof the directive metadata, as in this example:",
"translation": "另外,还可以在指令元数据的`inputs`或`outputs`数组中标记出这些成员。比如这个例子:",
@@ -25039,6 +26949,31 @@
"translation": "回到顶部 ",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
},
+ {
+ "original": "## The `$any` type cast function (`$any( )`)",
+ "translation": "## 类型转换函数 `$any` ($any( <表达式> ))",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "Sometimes a binding expression will be reported as a type error and it is not possible or difficult\nto fully specify the type. To silence the error, you can use the `$any` cast function to cast\nthe expression to [the `any` type](http://www.typescriptlang.org/docs/handbook/basic-types.html#any).",
+ "translation": "有时候,绑定表达式可能会报类型错误,并且它不能或很难指定类型。要消除这种报错,你可以使用 `$any` 转换函数来把表达式转换成 [`any` 类型](http://www.typescriptlang.org/docs/handbook/basic-types.html#any)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "In this example, when the Angular compiler turns your template into TypeScript code, \nit prevents TypeScript from reporting that `marker` is not a member of the `Hero`\ninterface.",
+ "translation": "在这个例子中,当 Angular 编译器把模板转换成 TypeScript 代码时,`$any` 表达式可以防止 TypeScript 编译器报错说 `marker` 不是 `Hero` 接口的成员。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "The `$any` cast function can be used in conjunction with `this` to allow access to undeclared members of\nthe component.",
+ "translation": "`$any` 转换函数可以和 `this` 联合使用,以便访问组件中未声明过的成员。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
+ {
+ "original": "The `$any` cast function can be used anywhere in a binding expression where a method call is valid.",
+ "translation": "`$any` 转换函数可以在绑定表达式中任何可以进行方法调用的地方使用。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/template-syntax.md"
+ },
{
"original": "## Summary",
"translation": "## 小结",
@@ -25059,24 +26994,844 @@
"translation": "# 测试",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "This guide offers tips and techniques for unit and integration testing Angular applications.",
+ "translation": "该指南提供了对 Angular 应用进行单元测试和集成测试的技巧和提示。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The guide presents tests of a sample CLI application that is much like the [_Tour of Heroes_ tutorial](tutorial).\nThe sample application and all tests in this guide are available for inspection and experimentation:",
+ "translation": "该指南中的测试面向的是一个很像[《英雄指南》教程](tutorial)的 CLI 范例应用。\n这个范例应用及其所有测试都可以在下面的链接中进行查看和试用:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* Sample app ",
+ "translation": "范例应用 ",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* Tests ",
+ "translation": "测试 ",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "## Setup",
"translation": "## 准备工作",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The Angular CLI downloads and install everything you need to test an Angular application with the [Jasmine test framework](http://jasmine.github.io/2.4/introduction.html).",
+ "translation": "Angular CLI 会下载并安装试用 [Jasmine 测试框架](http://jasmine.github.io/2.4/introduction.html) 测试 Angular 应用时所需的一切。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The project you create with the CLI is immediately ready to test.\nJust run this one CLI command:",
+ "translation": "你使用 CLI 创建的项目是可以立即用于测试的。\n运行下列 CLI 命令即可:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `ng test` command builds the app in _watch mode_,\nand launches the [karma test runner](https://karma-runner.github.io/1.0/index.html).",
+ "translation": "`ng test` 命令在*监视模式*下构建应用,并启动 [karma 测试运行器](https://karma-runner.github.io/1.0/index.html)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The console output looks a bit like this:",
+ "translation": "它的控制台输出一般是这样的:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The last line of the log is the most important. \nIt shows that Karma ran three tests that all passed.",
+ "translation": "最后一行很重要。它表示 Karma 运行了三个测试,而且这些测试都通过了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A chrome browser also opens and displays the test output in the \"Jasmine HTML Reporter\" like this.",
+ "translation": "它还会打开 Chrome 浏览器并在“ Jasmine HTML 报告器”中显示测试输出,就像这样:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Most people find this browser output easier to read than the console log.\nYou can click on a test row to re-run just that test or click on a description to re-run the tests in the selected test group (\"test suite\").",
+ "translation": "大多数人都会觉得浏览器中的报告比控制台中的日志更容易阅读。\n你可以点击一行测试,来单独重跑这个测试,或者点击一行描述信息来重跑所选测试组(“测试套件”)中的那些测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Meanwhile, the `ng test` command is watching for changes.",
+ "translation": "同时,`ng test` 命令还会监听这些变化。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "To see this in action, make a small change to `app.component.ts` and save.\nThe tests run again, the browser refreshes, and the new test results appear.",
+ "translation": "要查看它的实际效果,就对 `app.component.ts` 做一个小修改,并保存它。\n这些测试就会重新运行,浏览器也会刷新,然后新的测试结果就出现了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Configuration",
+ "translation": "#### 配置",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The CLI takes care of Jasmine and karma configuration for you.",
+ "translation": "CLI 会为你生成 Jasmine 和 Karma 的配置文件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You can fine-tune many options by editing the `karma.conf.js` file in the project root folder and\nthe `test.ts` file in the `src/` folder.",
+ "translation": "不过你也可以通过编辑项目根目录下的 `karma.conf.js` 文件以及 `src/` 目录下的 `test.ts` 文件来微调很多选项。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `karma.conf.js` file is a partial karma configuration file.\nThe CLI constructs the full runtime configuration in memory,based on application structure specified in the `.angular-cli.json` file, supplemented by `karma.conf.js`.",
+ "translation": "`karma.conf.js` 文件是 karma 配置文件的一部分。\nCLI 会基于 `.angular-cli.json` 文件中指定的项目结构和 `karma.conf.js` 文件,来在内存中构建出完整的运行时配置。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Search the web for more details about Jasmine and karma configuration.",
+ "translation": "要进一步了解 Jasmine 和 Karma 的配置项,请搜索网络。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Other test frameworks",
+ "translation": "#### 其它测试框架",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You can also unit test an Angular app with other testing libraries and test runners.\nEach library and runner has its own distinctive installation procedures, configuration, and syntax.",
+ "translation": "你还可以使用其它的测试库和测试运行器来对 Angular 应用进行单元测试。\n每个库和运行器都有自己特有的安装过程、配置项和语法。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Search the web to learn more.",
+ "translation": "要了解更多,请搜索网络。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Test file name and location",
+ "translation": "#### 测试文件名及其位置",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Look inside the `src/app` folder.",
+ "translation": "查看 `src/app` 文件夹。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The CLI generated a test file for the `AppComponent` named `app.component.spec.ts`.",
+ "translation": "CLI 为 `AppComponent` 生成了一个名叫 `app.component.spec.ts` 的测试文件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test file extension **must be `.spec.ts`** so that tooling can identify it as a file with tests (AKA, a _spec_ file).",
+ "translation": "测试文件的扩展名**必须是`.spec.ts`**,这样工具才能识别出它是一个测试文件,也叫规约(spec)文件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `app.component.ts` and `app.component.spec.ts` files are siblings in the same folder.\nThe root file names (`app.component`) are the same for both files.",
+ "translation": "`app.component.ts` 和 `app.component.spec.ts` 文件位于同一个文件夹中,而且相邻。\n其根文件名部分(`app.component`)都是一样的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Adopt these two conventions in your own projects for _every kind_ of test file.",
+ "translation": "请在你的项目中对*任意类型*的测试文件都坚持这两条约定。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "## Service Tests",
+ "translation": "## 对服务的测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Services are often the easiest files to unit test.\nHere are some synchronous and asynchronous unit tests of the `ValueService`\nwritten without assistance from Angular testing utilities.",
+ "translation": "服务通常是单元测试中最简单的文件类型。\n下面是一些针对 `ValueService` 的同步和异步单元测试,\n编写它们时没有借助来自 Angular 测试工具集的任何协助。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Services with dependencies",
+ "translation": "#### 带有依赖的服务",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Services often depend on other services that Angular injects into the constructor.\nIn many cases, it easy to create and _inject_ these dependencies by hand while\ncalling the service's constructor.",
+ "translation": "服务通常会依赖于一些 Angular 注入到其构造函数中的其它服务。\n多数情况下,创建并在调用该服务的构造函数时,手工创建并注入这些依赖也是很容易的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `MasterService` is a simple example:",
+ "translation": "`MasterService` 就是一个简单的例子:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "`MasterService` delegates its only method, `getValue`, to the injected `ValueService`.",
+ "translation": "`MasterService` 把它唯一的方法 `getValue` 委托给了注入进来的 `ValueService`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Here are several ways to test it.",
"translation": "这里是几种测试它的方法。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The first test creates a `ValueService` with `new` and passes it to the `MasterService` constructor.",
+ "translation": "第一个测试使用 `new` 创建了 `ValueService`,然后把它传给了 `MasterService` 的构造函数。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "However, injecting the real service rarely works well as most dependent services are difficult to create and control.",
+ "translation": "不过,对于大多数没这么容易创建和控制的依赖项来说,注入真实的服务很容易出问题。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Instead you can mock the dependency, use a dummy value, or create a \n[spy](https://jasmine.github.io/2.0/introduction.html#section-Spies) \non the pertinent service method.",
+ "translation": "你可以改用模拟依赖的方式,你可以使用虚值或在相关的服务方法上创建一个[间谍(spy)](https://jasmine.github.io/2.0/introduction.html#section-Spies)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Prefer spies as they are usually the easiest way to mock services.",
+ "translation": "优先使用间谍,因为它们通常是 Mock 服务时最简单的方式。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "These standard testing techniques are great for unit testing services in isolation.",
+ "translation": "这些标准的测试技巧对于在隔离的环境下对服务进行单元测试非常重要。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "However, you almost always inject service into application classes using Angular\ndependency injection and you should have tests that reflect that usage pattern.\nAngular testing utilities make it easy to investigate how injected services behave.",
+ "translation": "不过,你几乎迟早要用 Angular 的依赖注入机制来把服务注入到应用类中去,而且你应该已经有了这类测试。\nAngular 的测试工具集可以让你轻松探查这种注入服务的工作方式。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Testing services with the _TestBed_",
+ "translation": "#### 使用 `TestBed`(测试机床)测试服务",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Your app relies on Angular [dependency injection (DI)](guide/dependency-injection) \nto create services.\nWhen a service has a dependent service, DI finds or creates that dependent service.\nAnd if that dependent service has its own dependencies, DI finds-or-creates them as well.",
+ "translation": "你的应用中会依赖 Angular 的[依赖注入 (DI)](guide/dependency-injection) 来创建服务。\n当某个服务依赖另一个服务时,DI 就会找到或创建那个被依赖的服务。\n如果那个被依赖的服务还有它自己的依赖,DI 也同样会找到或创建它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "As service _consumer_, you don't worry about any of this.\nYou don't worry about the order of constructor arguments or how they're created.",
+ "translation": "作为服务的*消费方*,你不需要关心这些细节。\n你不用关心构造函数中的参数顺序或如何创建它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "As a service _tester_, you must at least think about the first level of service dependencies\nbut you _can_ let Angular DI do the service creation and deal with constructor argument order\nwhen you use the `TestBed` testing utility to provide and create services.",
+ "translation": "但对于服务的*测试方*来说,你就至少要考虑服务的第一级依赖了。\n不过你*可以*让 Angular DI 来负责服务的创建工作,但当你使用 `TestBed` 测试工具来提供和创建服务时,你仍然需要关心构造函数中的参数顺序。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `TestBed` is the most important of the Angular testing utilities.\nThe `TestBed` creates a dynamically-constructed Angular _test_ module that emulates\nan Angular [@NgModule](guide/ngmodules).",
+ "translation": "`TestBed` 是 Angular 测试工具中最重要的部分。\n`TestBed` 会动态创建一个用来模拟 [@NgModule](guide/ngmodules) 的 Angular *测试*模块。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `TestBed.configureTestingModule()` method takes a metadata object that can have most of the properties of an [@NgModule](guide/ngmodules).",
+ "translation": "`TestBed.configureTestingModule()` 方法接收一个元数据对象,其中具有 [@NgModule](guide/ngmodules) 中的绝大多数属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "To test a service, you set the `providers` metadata property with an\narray of the services that you'll test or mock.",
+ "translation": "要测试某个服务,就要在元数据的 `providers` 属性中指定一个将要进行测试或模拟的相关服务的数组。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Then inject it inside a test by calling `TestBed.get()` with the service class as the argument.",
+ "translation": "然后通过调用 `TestBed.get()`(参数为该服务类)把它注入到一个测试中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Or inside the `beforeEach()` if you prefer to inject the service as part of your setup.",
+ "translation": "或者,如果你更倾向于把该服务作为环境准备过程的一部分,就把它放在 `beforeEach()` 中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "When testing a service with a dependency, provide the mock in the `providers` array.",
+ "translation": "如果要测试一个带有依赖项的服务,那就把模拟对象放在 `providers` 数组中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In the following example, the mock is a spy object.",
+ "translation": "在下面的例子中,模拟对象是一个间谍(spy)对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test consumes that spy in the same way it did earlier.",
+ "translation": "该测试会像以前一样消费这个间谍对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Testing without _beforeEach()_",
+ "translation": "#### 不使用 `beforeEach` 进行测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Most test suites in this guide call `beforeEach()` to set the preconditions for each `it()` test\nand rely on the `TestBed` to create classes and inject services.",
+ "translation": "本指南中的大多数的测试套件都会调用 `beforeEach()` 来为每个 `it()` 测试准备前置条件,并依赖 `TestBed` 来创建类和注入服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "There's another school of testing that never calls `beforeEach()` and\nand prefers to create classes explicitly rather than use the `TestBed`.",
+ "translation": "另一些测试教程中也可能让你不要调用 `beforeEach()`,并且更倾向于显式创建类,而不要借助 `TestBed`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's how you might rewrite one of the `MasterService` tests in that style.",
+ "translation": "下面的例子教你如何把 `MasterService` 的测试改写成那种风格。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Begin by putting re-usable, preparatory code in a _setup_ function instead of `beforeEach()`.",
+ "translation": "通过把可复用的准备代码放进一个单独的 `setup` 函数来代替 `beforeEach()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `setup()` function returns an object literal \nwith the variables, such as `masterService`, that a test might reference.\nYou don't define _semi-global_ variables (e.g., `let masterService: MasterService`) \nin the body of the `describe()`.",
+ "translation": "`setup()` 函数返回一个带有一些变量的对象字面量,比如 `masterService`,测试中可以引用它。\n这样你就不用在 `describe()` 中定义一些*半全局性的*变量了(比如 `let masterService: MasterService` )。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Then each test invokes `setup()` in its first line, before continuing\nwith steps that manipulate the test subject and assert expectations.",
+ "translation": "然后,每个测试都会在第一行调用 `setup()`,然后再操纵被测主体以及对期望值进行断言。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Notice how the test uses \n[_destructuring assignment_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) \nto extract the setup variables that it needs.",
+ "translation": "注意这些测试是如何使用 [解构赋值](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)\n来提取出所需变量的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Many developers feel this approach is cleaner and more explicit than the\ntraditional `beforeEach()` style.",
+ "translation": "很多开发者觉得这种方式相比传统的 `beforeEach()` 风格更加干净、更加明确。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Although this testing guide follows the tradition style and \nthe default [CLI schematics](https://github.com/angular/devkit) \ngenerate test files with `beforeEach()` and `TestBed`,\nfeel free to adopt _this alternative approach_ in your own projects.",
+ "translation": "虽然本章会遵循传统的风格,并且 [CLI](https://github.com/angular/devkit) 生成的默认测试文件也用的是\n`beforeEach()` 和 `TestBed`,不过你可以在自己的项目中自由选择*这种可选方式*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Testing HTTP services",
+ "translation": "#### 测试 HTTP 服务",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Data services that make HTTP calls to remote servers typically inject and delegate \nto the Angular [`HttpClient`](guide/http) service for XHR calls.",
+ "translation": "那些会向远端服务器发起 HTTP 调用的数据服务,通常会注入 Angular 的 [`HttpClient`](guide/http) 服务并委托它进行 XHR 调用。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You can test a data service with an injected `HttpClient` spy as you would\ntest any service with a dependency.",
+ "translation": "你可以像测试其它带依赖的服务那样,通过注入一个 `HttpClient` 间谍来测试这种数据服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `HeroService` methods return _Observables_.\n_Subscribe_ to the method observable to (a) cause it to execute and (b)\nassert that the method succeeds or fails.",
+ "translation": "`HttpService` 中的方法会返回*可观察对象*。*订阅*这些方法返回的可观察对象会让它开始执行,并且断言这些方法是成功了还是失败了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `subscribe()` method takes a success and fail callback.\nMake sure you provide _both_ callbacks so that you capture errors.",
+ "translation": "`subscribe()` 方法接受一个成功回调和一个失败回调。\n你要确保同时提供了这两个回调,以便捕获错误。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Neglecting to do so produces an asynchronous uncaught observable error that\nthe test runner will likely attribute to a completely different test.",
+ "translation": "如果忽略这些异步调用中未捕获的错误,测试运行器可能会得出截然不同的测试结论。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Extended interactions between a data service and the `HttpClient` can be complex\nand difficult to mock with spies.",
+ "translation": "如果将来 `HttpClient` 和数据服务之间有更多的交互,则可能会变得复杂,而且难以使用间谍进行模拟。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `HttpClientTestingModule` can make these testing scenarios more manageable.",
+ "translation": "`HttpClientTestingModule` 可以让这些测试场景变得更加可控。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "While the _code sample_ accompanying this guide demonstrates `HttpClientTestingModule`,\nthis page defers to the [Http guide](guide/http#testing-http-requests),\nwhich covers testing with the `HttpClientTestingModule` in detail.",
+ "translation": "本章的*代码范例*要示范的是 `HttpClientTestingModule`,所以把部分内容移到了 [HttpClient 中](guide/http#testing-http-requests),那里会详细讲解如何用 `HttpClientTestingModule` 进行测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This guide's sample code also demonstrates testing of the _legacy_ `HttpModule`\nin `app/model/http-hero.service.spec.ts`.",
+ "translation": "本章范例代码中的 `app/model/http-hero.service.spec.ts` 还示范了如何使用*传统的* `HttpModule` 进行验证。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "## Component Test Basics",
+ "translation": "## 组件测试基础",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A component, unlike all other parts of an Angular application,\ncombines an HTML template and a TypeScript class.\nThe component truly is the template and the class _working together_.\nand to adequately test a component, you should test that they work together\nas intended.",
+ "translation": "组件与 Angular 应用中的其它部分不同,它是由 HTML 模板和 TypeScript 类组成的。\n组件其实是指模板加上与其合作的类。\n要想对组件进行充分的测试,就要测试它们能否如预期的那样协作。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Such tests require creating the component's host element in the browser DOM,\nas Angular does, and investigating the component class's interaction with\nthe DOM as described by its template.",
+ "translation": "这些测试需要在浏览器的 DOM 中创建组件的宿主元素(就像 Angular 所做的那样),然后检查组件类和 DOM 的交互是否如同它在模板中所描述的那样。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The Angular `TestBed` facilitates this kind of testing as you'll see in the sections below.\nBut in many cases, _testing the component class alone_, without DOM involvement,\ncan validate much of the component's behavior in an easier, more obvious way.",
+ "translation": "Angular 的 `TestBed` 为所有这些类型的测试提供了基础设施。\n但是很多情况下,可以*单独测试组件类本身*而不必涉及 DOM,就已经可以用一种更加简单、清晰的方式来验证该组件的大多数行为了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Component class testing",
+ "translation": "### 单独测试组件类",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Test a component class on its own as you would test a service class.",
+ "translation": "你可以像测试服务类一样测试组件类。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Consider this `LightswitchComponent` which toggles a light on and off\n(represented by an on-screen message) when the user clicks the button.",
+ "translation": "考虑下面这个 `LightswitchComponent`,当用户点击按钮时,它会切换灯的开关状态(用屏幕上的消息展现出来)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You might decide only to test that the `clicked()` method\ntoggles the light's _on/off_ state and sets the message appropriately.",
+ "translation": "你可能要测试 `clicked()` 方法能否正确切换灯的开关状态。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This component class has no dependencies.\nTo test a service with no dependencies, you create it with `new`, poke at its API,\nand assert expectations on its public state.\nDo the same with the component class.",
+ "translation": "该组件类没有依赖。\n要测试一个没有依赖的服务,你会用 `new` 来创建它,调用它的 API,然后对它的公开状态进行断言。\n组件类也可以这么做。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here is the `DashboardHeroComponent` from the _Tour of Heroes_ tutorial.",
+ "translation": "下面这段代码是来自《英雄指南》教程的 `DashboardHeroComponent`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "It appears within the template of a parent component,\nwhich binds a _hero_ to the `@Input` property and\nlistens for an event raised through the _selected_ `@Output` property.",
+ "translation": "它呈现在父组件的模板中,那里把一个英雄绑定到了 `@Input` 属性上,并且通过 `@Output` 属性监听*选中英雄*时的事件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You can test that the class code works without creating the the `DashboardHeroComponent`\nor its parent component.",
+ "translation": "你可以测试 `DashboardHeroComponent` 类,而不用完整创建它或其父组件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "When a component has dependencies, you may wish to use the `TestBed` to both\ncreate the component and its dependencies.",
+ "translation": "当组件有依赖时,你可能要使用 `TestBed` 来同时创建该组件及其依赖。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The following `WelcomeComponent` depends on the `UserService` to know the name of the user to greet.",
+ "translation": "下面的 `WelcomeComponent` 依赖于 `UserService`,并通过它知道要打招呼的那位用户的名字。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You might start by creating a mock of the `UserService` that meets the minimum needs of this component.",
+ "translation": "你可能要先创建一个满足本组件最小需求的模拟板 `UserService`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Then provide and inject _both the_ **component** _and the service_ in the `TestBed` configuration.",
+ "translation": "然后在 `TestBed` 的配置中提供并注入该**组件**和该**服务**。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Then exercise the component class, remembering to call the [lifecycle hook methods](guide/lifecycle-hooks) as Angular does when running the app.",
+ "translation": "然后使用这个组件类,别忘了像 Angular 运行本应用时那样调用它的[生命周期钩子方法](guide/lifecycle-hooks)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Component DOM testing",
+ "translation": "### 组件 DOM 的测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Testing the component _class_ is as easy as testing a service.",
+ "translation": "测试组件*类*就像测试服务那样简单。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "But a component is more than just its class.\nA component interacts with the DOM and with other components.\nThe _class-only_ tests can tell you about class behavior.\nThey cannot tell you if the component is going to render properly,\nrespond to user input and gestures, or integrate with its parent and and child components.",
+ "translation": "但组件不仅是这个类。\n组件还要和 DOM 以及其它组件进行交互。\n*只涉及类*的测试可以告诉你组件类的行为是否正常,\n但是不能告诉你组件是否能正常渲染出来、响应用户的输入和查询或与它的父组件和子组件相集成。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "None of the _class-only_ tests above can answer key questions about how the\ncomponents actually behave on screen.",
+ "translation": "上述*只涉及类*的测试没办法回答这些组件在屏幕上的行为之类的关键性问题:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* Is `Lightswitch.clicked()` bound to anything such that the user can invoke it?",
+ "translation": "`Lightswitch.clicked()` 是否真的绑定到了某些用户可以接触到的东西?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* Is the `Lightswitch.message` displayed?",
+ "translation": "`Lightswitch.message` 是否真的显示出来了?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* Can the user actually select the hero displayed by `DashboardHeroComponent`?",
+ "translation": "用户真的可以选择 `DashboardHeroComponent` 中显示的某个英雄吗?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* Is the hero name displayed as expected (i.e, in uppercase)?",
+ "translation": "英雄的名字是否如预期般显示出来了?(比如是否大写)",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* Is the welcome message displayed by the template of `WelcomeComponent`?",
+ "translation": "`WelcomeComponent` 的模板是否显示了欢迎信息?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "These may not be troubling questions for the simple components illustrated above.\nBut many components have complex interactions with the DOM elements\ndescribed in their templates, causing HTML to appear and disappear as\nthe component state changes.",
+ "translation": "这些问题对于上面这种简单的组件来说当然没有问题,\n不过很多组件和它们模板中所描述的 DOM 元素之间会有复杂的交互,当组件的状态发生变化时,会导致一些 HTML 出现和消失。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "To answer these kinds of questions, you have to create the DOM elements associated\nwith the components, you must examine the DOM to confirm that component state\ndisplays properly at the appropriate times, and you must simulate user interaction\nwith the screen to determine whether those interactions cause the component to\nbehave as expected.",
+ "translation": "要回答这类问题,你就不得不创建那些与组件相关的 DOM 元素了,你必须检查 DOM 来确认组件的状态能在恰当的时机正常显示出来,并且必须通过屏幕来仿真用户的交互,以判断这些交互是否如预期的那般工作。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "To write these kinds of test, you'll use additional features of the `TestBed`\nas well as other testing helpers.",
+ "translation": "要想写这类测试,你就要用到 `TestBed` 的附加功能以及其它测试助手了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### CLI-generated tests",
+ "translation": "#### CLI 生成的测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The CLI creates an initial test file for you by default when you ask it to\ngenerate a new component.",
+ "translation": "当你用 CLI 生成新的组件时,它也会默认创建最初的测试文件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "For example, the following CLI command generates a `BannerComponent` in the `app/banner` folder (with inline template and styles):",
+ "translation": "比如,下列 CLI 命令会在 `app/banner` 文件夹中创建带有内联模板和内联样式的 `BannerComponent`:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "It also generates an initial test file for the component, `banner-external.component.spec.ts`, that looks like this:",
+ "translation": "它也会为组件生成最初的测试文件 `banner-external.component.spec.ts`,代码如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Reduce the setup",
+ "translation": "#### 缩减准备代码",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Only the last three lines of this file actually test the component\nand all they do is assert that Angular can create the component.",
+ "translation": "这个文件中只有最后三行是真正测试组件的,它们用来断言 Angular 可以创建该组件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The rest of the file is boilerplate setup code anticipating more advanced tests that _might_ become necessary if the component evolves into something substantial.",
+ "translation": "文件的其它部分都是为更高级的测试二准备的样板代码,当组件逐渐演变成更加实质性的东西时,它们才*可能*变成必备的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You'll learn about these advanced test features below.\nFor now, you can radically reduce this test file to a more manageable size:",
+ "translation": "稍后你讲学到这些高级的测试特性。\n不过目前,你可以先把这些测试文件缩减成更加可控的大小,以便理解:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In this example, the metadata object passed to `TestBed.configureTestingModule` \nsimply declares `BannerComponent`, the component to test.",
+ "translation": "在这个例子中,传给 `TestBed.configureTestingModule` 的元数据对象中只声明了 `BannerComponent` —— 我们要测试的组件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "There's no need to declare or import anything else.\nThe default test module is pre-configured with \nsomething like the `BrowserModule` from `@angular/platform-browser`.",
+ "translation": "不用声明或导入任何其它的东西。\n默认的测试模块中已经预先配置好了一些东西,比如来自 `@angular/platform-browser` 的 `BrowserModule`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Later you'll call `TestBed.configureTestingModule()` with\nimports, providers, and more declarations to suit your testing needs.\nOptional `override` methods can further fine-tune aspects of the configuration.",
+ "translation": "稍后你将会调用带有导入模块、服务提供商和更多可声明对象的 `TestBed.configureTestingModule()` 来满足测试所需。\n将来还可以用可选的 `override` 方法对这些配置进行微调。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "After configuring `TestBed`, you call its `createComponent()` method.",
+ "translation": "在配置好 `TestBed` 之后,你还可以调用它的 `createComponent()` 方法。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "`TestBed.createComponent()` creates an instance of the `BannerComponent`, \nadds a corresponding element to the test-runner DOM,\nand returns a [`ComponentFixture`](#component-fixture).",
+ "translation": "`TestBed.createComponent()` 会创建一个 `BannerComponent` 的实例,把相应的元素添加到测试运行器的 DOM 中,然后返回一个 [`ComponentFixture`](#component-fixture) 对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Do not re-configure `TestBed` after calling `createComponent`.",
- "translation": "在调用了`createComponent`之后就不要再重新配置`TestBed`了。",
+ "translation": "在调用了`createComponent`之后就不能再重新配置`TestBed`了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `createComponent` method freezes the current `TestBed`definition, \nclosing it to further configuration.",
+ "translation": "`createComponent` 方法冻结了当前的 `TestBed` 定义,关闭它才能再进行后续配置。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You cannot call any more `TestBed` configuration methods, not `configureTestingModule()`,\nnor `get()`, nor any of the `override...` methods.\nIf you try, `TestBed` throws an error.",
+ "translation": "你不能再调用任何 `TestBed` 的后续配置方法了,不能调 `configureTestingModule()`、不能调 `get()`,\n也不能调用任何 `override...` 方法。\n如果试图这么做,`TestBed` 就会抛出错误。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The [ComponentFixture](api/core/testing/ComponentFixture) is a test harness for interacting with the created component and its corresponding element.",
+ "translation": "[ComponentFixture](api/core/testing/ComponentFixture) 是一个测试挽具(就像马车缰绳),用来与所创建的组件及其 DOM 元素进行交互。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Access the component instance through the fixture and confirm it exists with a Jasmine expectation:",
+ "translation": "可以通过测试夹具(fixture)来访问该组件的实例,并用 Jasmine 的 `expect` 语句来确保其存在。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You will add more tests as this component evolves.\nRather than duplicate the `TestBed` configuration for each test,\nyou refactor to pull the setup into a Jasmine `beforeEach()` and some supporting variables:",
+ "translation": "随着该组件的成长,你将会添加更多测试。\n除了为每个测试都复制一份 `TestBed` 测试之外,你还可以把它们重构成 Jasmine 的 `beforeEach()` 中的准备语句以及一些支持性变量:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Now add a test that gets the component's element from `fixture.nativeElement` and \nlooks for the expected text.",
+ "translation": "现在,添加一个测试,用它从 `fixture.nativeElement` 中获取组件的元素,并查找是否存在所预期的文本内容。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The value of `ComponentFixture.nativeElement` has the `any` type.\nLater you'll encounter the `DebugElement.nativeElement` and it too has the `any` type.",
+ "translation": "`ComponentFixture.nativeElement` 的值是 `any` 类型的。\n稍后你将遇到的 `DebugElement.nativeElement` 也同样是 `any` 类型的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Angular can't know at compile time what kind of HTML element the `nativeElement` is or \nif it even is an HTML element.\nThe app might be running on a _non-browser platform_, such as the server or a\n[Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API),\nwhere the element may have a diminished API or not exist at all.",
+ "translation": "Angular 在编译期间没办法知道 `nativeElement` 是哪种 HTML 元素,甚至是否 HTML 元素(译注:比如可能是 SVG 元素)。\n本应用还可能运行在*非浏览器平台上*,比如服务端渲染或 [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) 那里的元素可能只有一些缩水过的 API,甚至根本不存在。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The tests in this guide are designed to run in a browser so a\n`nativeElement` value will always be an `HTMLElement` or \none of its derived classes.",
+ "translation": "本指南中的例子都是为运行在浏览器中而设计的,因此 `nativeElement` 的值一定会是 `HTMLElement` 及其派生类。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Knowing that it is an `HTMLElement` of some sort, you can use\nthe standard HTML `querySelector` to dive deeper into the element tree.",
+ "translation": "如果知道了它是某种 `HTMLElement`,你就可以用标准的 `querySelector` 在元素树中进行深挖了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's another test that calls `HTMLElement.querySelector` to get the paragraph element and look for the banner text:",
+ "translation": "下面这个测试就会调用 `HTMLElement.querySelector` 来获取 `` 元素,并在其中查找 Banner 文本:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The Angular _fixture_ provides the component's element directly through the `fixture.nativeElement`.",
+ "translation": "Angular 的*夹具*可以通过 `fixture.nativeElement` 直接提供组件的元素。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This is actually a convenience method, implemented as `fixture.debugElement.nativeElement`.",
+ "translation": "它实际上是 `fixture.debugElement.nativeElement` 的一个便利方法。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "There's a good reason for this circuitous path to the element.",
+ "translation": "这种访问元素的迂回方式有很好的理由。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The properties of the `nativeElement` depend upon the runtime environment.\nYou could be running these tests on a _non-browser_ platform that doesn't have a DOM or \nwhose DOM-emulation doesn't support the full `HTMLElement` API.",
+ "translation": "`nativeElement` 的属性取决于运行环境。\n你可以在没有 DOM,或者其 DOM 模拟器无法支持全部 `HTMLElement` API 的平台上运行这些测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Angular relies on the `DebugElement` abstraction to work safely across _all supported platforms_.\nInstead of creating an HTML element tree, Angular creates a `DebugElement` tree that wraps the _native elements_ for the runtime platform.\nThe `nativeElement` property unwraps the `DebugElement` and returns the platform-specific element object.",
+ "translation": "Angular 依赖于 `DebugElement` 这个抽象层,就可以安全的横跨*其支持的所有平台*。\nAngular 不再创建 HTML 元素树,而是创建 `DebugElement` 树,其中包裹着相应运行平台上的*原生元素*。\n`nativeElement` 属性会解开 `DebugElement`,并返回平台相关的元素对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Because the sample tests for this guide are designed to run only in a browser,\na `nativeElement` in these tests is always an `HTMLElement`\nwhose familiar methods and properties you can explore within a test.",
+ "translation": "因为本章的这些测试都设计为只运行在浏览器中,因此这些测试中的 `nativeElement` 总是 `HTMLElement`,\n你可以在测试中使用那些熟悉的方法和属性进行浏览。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's the previous test, re-implemented with `fixture.debugElement.nativeElement`:",
+ "translation": "下面是对上一个测试改用 `fixture.debugElement.nativeElement` 进行的重新实现:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `DebugElement` has other methods and properties that\nare useful in tests, as you'll see elsewhere in this guide.",
+ "translation": "`DebugElement` 还有其它的方法和属性,它们在测试中也很有用,你将在本章的其它测试中看到它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You import the `DebugElement` symbol from the Angular core library.",
+ "translation": "你要从 Angular 核心库中导入 `DebugElement` 符号。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Although the tests in this guide all run in the browser,\nsome apps might run on a different platform at least some of the time.",
+ "translation": "虽然本章中的测试都是运行在浏览器中的,不过有些应用可能会运行在其它平台上(至少一部分时间是这样)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "For example, the component might render first on the server as part of a strategy to make the application launch faster on poorly connected devices. The server-side renderer might not support the full HTML element API.\nIf it doesn't support `querySelector`, the previous test could fail.",
+ "translation": "比如,作为加快慢速网络设备上应用启动速度的一种策略,组件可能要先在服务器上渲染。服务端渲染可能无法支持完全的 HTML API。\n如果它不支持 `querySelector`,那么前一个测试就会失败。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `DebugElement` offers query methods that work for all supported platforms.\nThese query methods take a _predicate_ function that returns `true` when a node in the `DebugElement` tree matches the selection criteria.",
+ "translation": "`DebugElement` 提供了可以工作在所有受支持的平台上的查询方法。\n这些查询方法接受一个谓词(predicate)函数,如果 `DebugElement` 树中的节点满足某个筛选条件,它就返回 `true`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You create a _predicate_ with the help of a `By` class imported from a\nlibrary for the runtime platform. Here's the `By` import for the browser platform:",
+ "translation": "你可以在从库中导入的 `By` 类的帮助下为该运行平台创建谓词函数。下面这个 `By` 是从浏览器平台导入的:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The following example re-implements the previous test with\n`DebugElement.query()` and the browser's `By.css` method.",
+ "translation": "下面这个例子使用 `DebugElement.query()` 和浏览器的 `By.css` 方法重新实现了前一个测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Some noteworthy observations:",
+ "translation": "值得注意的地方有:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* The `By.css()` static method selects `DebugElement` nodes \nwith a [standard CSS selector](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors \"CSS selectors\").",
+ "translation": "`By.css()` 静态方法使用[标准 CSS 选择器](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors \"CSS selectors\")选择了一些 `DebugElement` 节点。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* The query returns a `DebugElement` for the paragraph.",
+ "translation": "这次查询返回了 `
` 元素的一个 `DebugElement`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* You must unwrap that result to get the paragraph element.",
+ "translation": "你必须解包此结果,以获取这个 `
` 元素。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "When you're filtering by CSS selector and only testing properties of a browser's _native element_, the `By.css` approach may be overkill.",
+ "translation": "当你要通过 CSS 选择器过滤,并且只打算测试浏览器的*原生元素*的属性时,`By.css` 这种方法就有点多余了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "It's often easier and more clear to filter with a standard `HTMLElement` method \nsuch as `querySelector()` or `querySelectorAll()`, \nas you'll see in the next set of tests.",
+ "translation": "使用标准的 `HTMLElement` 方法,比如 `querySelector()` 或 `querySelectorAll()` 通常会更简单、更清晰。\n你在下一组测试中就会体会到这一点。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "## Component Test Scenarios",
+ "translation": "## 组件测试场景",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The following sections, comprising most of this guide, explore common\ncomponent testing scenarios",
+ "translation": "下面这些部分构成了本指南的大部分内容,它们将探讨一些常见的组件测试场景。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Component binding",
+ "translation": "### 组件绑定",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The current `BannerComponent` presents static title text in the HTML template.",
+ "translation": "当前的 `BannerComponent` 在 HTML 模板中展示了静态标题内容。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "After a few changes, the `BannerComponent` presents a dynamic title by binding to\nthe component's `title` property like this.",
+ "translation": "稍作修改之后,`BannerComponent` 也可以通过绑定到组件的 `title` 属性来展示动态标题。就像这样:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Simple as this is, you decide to add a test to confirm that component \nactually displays the right content where you think it should.",
+ "translation": "很简单,你决定添加一个测试来确定这个组件真的像你预期的那样显示出了正确的内容。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Query for the _<h1>_",
+ "translation": "#### 查询 `
`",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You'll write a sequence of tests that inspect the value of the `` element\nthat wraps the _title_ property interpolation binding.",
+ "translation": "你将会写一系列测试来探查 `` 元素的值,这个值包含在了带有 `title` 属性的插值表达式绑定中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You update the `beforeEach` to find that element with a standard HTML `querySelector`\nand assign it to the `h1` variable.",
+ "translation": "你要修改 `beforeEach` 来使用标准的 HTML `querySelector` 来找到钙元素,并把它赋值给 `h1` 变量。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### _createComponent()_ does not bind data",
+ "translation": "#### `createComponent()` 函数不会绑定数据",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "For your first test you'd like to see that the screen displays the default `title`.\nYour instinct is to write a test that immediately inspects the `` like this:",
+ "translation": "你的第一个测试希望看到屏幕显示出了默认的 `title`。\n你本能的写出如下测试来立即审查这个 `` 元素:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "_That test fails_ with the message:",
+ "translation": "但*测试失败了*,给出如下信息:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Binding happens when Angular performs **change detection**.",
+ "translation": "因为绑定是在 Angular 执行**变更检测**时才发生的。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
"original": "In production, change detection kicks in automatically\nwhen Angular creates a component or the user enters a keystroke or\nan asynchronous activity (e.g., AJAX) completes.",
- "translation": "在产品阶段,当Angular创建组件、用户输入或者异步动作(比如AJAX)完成时,自动触发变更检测。",
+ "translation": "在产品阶段,当Angular创建组件、用户输入或者异步动作(比如AJAX)完成时,会自动触发变更检测。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `TestBed.createComponent` does _not_ trigger change detection.\na fact confirmed in the revised test:",
+ "translation": "但 `TestBed.createComponent` *不能*触发变更检测。\n可以在这个修改后的测试中确定这一点:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You must tell the `TestBed` to perform data binding by calling `fixture.detectChanges()`.\nOnly then does the `` have the expected title.",
+ "translation": "你必须通过调用 `fixture.detectChanges()` 来要求 `TestBed` 执行数据绑定。\n然后 `` 中才会具有所期望的标题。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Delayed change detection is intentional and useful.\nIt gives the tester an opportunity to inspect and change the state of\nthe component _before Angular initiates data binding and calls [lifecycle hooks](guide/lifecycle-hooks)_.",
+ "translation": "这种迟到的变更检测是故意设计的,而且很有用。\n它给了测试者一个机会,*在 Angular 初始化数据绑定以及调用[生命周期钩子](guide/lifecycle-hooks)之前*探查并改变组件的状态。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's another test that changes the component's `title` property _before_ calling `fixture.detectChanges()`.",
+ "translation": "下面这个测试中,会在调用 `fixture.detectChanges()` *之前*修改组件的 `title` 属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Automatic change detection",
+ "translation": "#### 自动变更检测",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `BannerComponent` tests frequently call `detectChanges`.\nSome testers prefer that the Angular test environment run change detection automatically.",
+ "translation": "`BannerComponent` 的这些测试需要频繁调用 `detectChanges`。\n有些测试者更喜欢让 Angular 测试环境自动运行变更检测。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "That's possible by configuring the `TestBed` with the `ComponentFixtureAutoDetect` provider.\nFirst import it from the testing utility library:",
+ "translation": "使用 `ComponentFixtureAutoDetect` 服务提供商来配置 `TestBed` 就可以做到这一点。\n首先从测试工具库中导入它:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25096,12 +27851,57 @@
},
{
"original": "The second and third test reveal an important limitation.\nThe Angular testing environment does _not_ know that the test changed the component's `title`.\nThe `ComponentFixtureAutoDetect` service responds to _asynchronous activities_ such as promise resolution, timers, and DOM events.\nBut a direct, synchronous update of the component property is invisible.\nThe test must call `fixture.detectChanges()` manually to trigger another cycle of change detection.",
- "translation": "第二和第三个测试程序显示了一个重要的局限性。\nAngular测试环境**不会**知道测试程序改变了组件的`title`属性。\n自动检测只对异步行为比如承诺的解析、计时器和DOM事件作出反应。\n但是直接修改组件属性值的这种同步更新是不会触发**自动检测**的。\n测试程序必须手动调用`fixture.detectChange()`,来触发新一轮的变更检测周期。",
+ "translation": "第二和第三个测试程序显示了它重要的局限性。\nAngular测试环境**不会**知道测试程序改变了组件的`title`属性。\n自动检测只对异步行为比如承诺的解析、计时器和DOM事件作出反应。\n但是直接修改组件属性值的这种同步更新是不会触发**自动检测**的。\n测试程序必须手动调用`fixture.detectChange()`,来触发新一轮的变更检测周期。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
"original": "Rather than wonder when the test fixture will or won't perform change detection,\nthe samples in this guide _always call_ `detectChanges()` _explicitly_.\nThere is no harm in calling `detectChanges()` more often than is strictly necessary.",
- "translation": "与其怀疑测试工具会不会执行变更检测,本章中的例子**总是显式**调用`detectChanges()`。\n即使是在不需要的时候,频繁调用`detectChanges()`没有任何什么坏处。",
+ "translation": "相比于受测试工具有没有执行变更检测的困扰,本章中的例子更愿意**总是显式**调用 `detectChanges()`。\n即使是在不需要的时候,频繁调用 `detectChanges()` 也没有任何坏处。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Component with external files",
+ "translation": "### 带有外部文件的组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `BannerComponent` above is defined with an _inline template_ and _inline css_, specified in the `@Component.template` and `@Component.styles` properties respectively.",
+ "translation": "上面的 `BannerComponent` 定义了一个*内联模板*和*内联 CSS*,分别是在 `@Component.template` 和 `@Component.styles` 属性中指定的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Many components specify _external templates_ and _external css_ with the\n`@Component.templateUrl` and `@Component.styleUrls` properties respectively,\nas the following variant of `BannerComponent` does.",
+ "translation": "很多组件会分别用 `@Component.templateUrl` 和 `@Component.styleUrls` 属性来指定*外部模板*和*外部 CSS*,就像下面这个 `BannerComponent` 的变体中所做的一样:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This syntax tells the Angular compiler to read the external files during component compilation.",
+ "translation": "这个语法告诉 Angular 编译器在编译期间读取外部文件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "That's not a problem when you run the CLI `ng test` command because it\n_compiles the app before running the tests_.",
+ "translation": "当你运行 CLI 的 `ng test` 命令的时候这毫无问题,因为它会*在运行测试之前先编译该应用*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "However, if you run the tests in a **non-CLI environment**,\ntests of this component may fail.\nFor example, if you run the `BannerComponent` tests in a web coding environment such as [plunker](http://plnkr.co/), you'll see a message like this one:",
+ "translation": "不过,如果你在**非 CLI 环境下**运行这些测试,那么对该组件的测试就可能失败。\n比如,如果你在像 [plunker](http://plnkr.co/) 这样的 Web 编程环境下运行 `BannerComponent` 的测试,就会看到如下信息:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You get this test failure message when the runtime environment \ncompiles the source code _during the tests themselves_.",
+ "translation": "如果在*测试自身期间*,运行环境试图编译源码,就会出现这个测试错误信息。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "To correct the problem, call `compileComponents()` as explained [below](#compile-components).",
+ "translation": "要解决这个问题,可以像[稍后](#compile-components)解释的那样调用一次 `compileComponents()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Component with a dependency",
+ "translation": "### 带依赖的组件",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25114,14 +27914,24 @@
"translation": "`WelcomeComponent`为登陆的用户显示一条欢迎信息。它从注入的`UserService`的属性得知用户的身份:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The `WelcomeComponent` has decision logic that interacts with the service, logic that makes this component worth testing.\nHere's the testing module configuration for the spec file, `app/welcome/welcome.component.spec.ts`:",
+ "translation": "`WelcomeComponent` 带有与服务交互的决策逻辑,这些逻辑让该组件值得测试。\n下面是 `app/welcome/welcome.component.spec.ts` 中的测试模块配置:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "This time, in addition to declaring the _component-under-test_,\nthe configuration adds a `UserService` provider to the `providers` list.\nBut not the real `UserService`.",
"translation": "这次,在测试配置里不但声明了被测试的组件,而且在`providers`数组中添加了`UserService`依赖。但不是真实的`UserService`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### Provide service test doubles",
+ "translation": "#### 提供服务的测试替身",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "A _component-under-test_ doesn't have to be injected with real services.\nIn fact, it is usually better if they are test doubles (stubs, fakes, spies, or mocks).\nThe purpose of the spec is to test the component, not the service,\nand real services can be trouble.",
- "translation": "被测试的组件不一定要注入真正的服务。实际上,服务的替身(stubs, fakes, spies或者mocks)通常会更加合适。\nspec的主要目的是测试组件,而不是服务。真实的服务可能自身有问题。",
+ "translation": "被测试的组件不一定要注入真正的服务。实际上,服务的替身(Stub - 桩, Fake - 假冒品, Spy - 间谍或者 Mock - 模拟对象)通常会更加合适。\nspec 的主要目的是测试组件,而不是服务。真实的服务可能连自身都有问题,不应该让它干扰对组件的测试。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25129,6 +27939,16 @@
"translation": "注入真实的`UserService`有可能很麻烦。真实的服务可能询问用户登录凭据,也可能试图连接认证服务器。\n可能很难处理这些行为。所以在真实的`UserService`的位置创建和注册`UserService`替身,会让测试更加容易和安全。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "This particular test suite supplies a minimal mock of the `UserService` that satisfies the needs of the `WelcomeComponent`\nand its tests:",
+ "translation": "这个测试套件提供了 `UserService` 的一个最小化模拟对象,它能满足 `WelcomeComponent` 及其测试的需求:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Get injected services",
+ "translation": "#### 获取注入的服务",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The tests need access to the (stub) `UserService` injected into the `WelcomeComponent`.",
"translation": "测试程序需要访问被注入到`WelcomeComponent`中的`UserService`(stub类)。",
@@ -25141,7 +27961,27 @@
},
{
"original": "The safest way to get the injected service, the way that **_always works_**,\nis to **get it from the injector of the _component-under-test_**.\nThe component injector is a property of the fixture's `DebugElement`.",
- "translation": "最安全并总是有效的获取注入服务的方法,是从被测试的组件的注入器获取。\n组件注入器是fixture的`DebugElement`的属性。",
+ "translation": "最安全并**总是有效**的获取注入服务的方法,是**从被测组件的注入器获取**。\n组件注入器是fixture的`DebugElement`的属性之一。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You _may_ also be able to get the service from the root injector via `TestBed.get()`.\nThis is easier to remember and less verbose.\nBut it only works when Angular injects the component with the service instance in the test's root injector.",
+ "translation": "你也可能通过 `TestBed.get()` 来使用根注入器获取该服务。\n这样更容易记住而且不那么啰嗦。\n不过这只有当 Angular 组件需要的恰好是该测试的根注入器时才能正常工作。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In this test suite, the _only_ provider of `UserService` is the root testing module,\nso it is safe to call `TestBed.get()` as follows:",
+ "translation": "在这个测试套件中,`UserService` *唯一*的提供商就是根测试模块中的,因此调用 `TestBed.get()` 就是安全的,代码如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "For a use case in which `TestBed.get()` does not work,\nsee the section [_Override a component's providers_](#component-override), which\nexplains when and why you must get the service from the component's injector instead.",
+ "translation": "对于那些不能用 `TestBed.get()` 的测试用例,请参见[改写组件的提供商](#component-override)一节,那里解释了何时以及为何必须改从组件自身的注入器中获取服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Always get the service from an injector",
+ "translation": "#### 总是从注入其中获取服务",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25149,6 +27989,16 @@
"translation": "请不要引用测试代码里提供给测试模块的`userServiceStub`对象。**这样不行!**\n被注入组件的`userService`实例是完全**不一样**的对象,它提供的是`userServiceStub`的克隆。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### Final setup and tests",
+ "translation": "#### 最终的准备及测试代码",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's the complete `beforeEach()`, using `TestBed.get()`:",
+ "translation": "下面是使用 `TestBed.get()` 的完整的 `beforeEach()`:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "And here are some tests:",
"translation": "下面是一些测试程序:",
@@ -25159,11 +28009,416 @@
"translation": "第一个测试程序是合法测试程序,它确认这个被模拟的`UserService`是否被调用和工作正常。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The second parameter to the Jasmine matcher (e.g., `'expected name'`) is an optional failure label.\nIf the expectation fails, Jasmine displays appends this label to the expectation failure message.\nIn a spec with multiple expectations, it can help clarify what went wrong and which expectation failed.",
+ "translation": "Jasmine 匹配器的第二个参数(比如 `'expected name'`)是一个可选的失败标签。\n如果这个期待语句失败了,Jasmine 就会把这个标签追加到这条个期待语句的失败信息后面。\n对于具有多个期待语句的规约,它可以帮助澄清到底什么出错了,以及哪个期待语句失败了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The remaining tests confirm the logic of the component when the service returns different values.\nThe second test validates the effect of changing the user name.\nThe third test checks that the component displays the proper message when there is no logged-in user.",
"translation": "接下来的测试程序确认当服务返回不同的值时组件的逻辑是否工作正常。\n第二个测试程序验证变换用户名字的效果。\n第三个测试程序检查如果用户没有登录,组件是否显示正确消息。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "### Component with async service",
+ "translation": "### 带有异步服务的组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In this sample, the `AboutComponent` template hosts a `TwainComponent`.\nThe `TwainComponent` displays Mark Twain quotes.",
+ "translation": "在这个例子中,`AboutComponent` 的模板中还有一个 `TwainComponent`。\n`TwainComponent` 用于显示引自马克·吐温的话。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Note that value of the component's `quote` property passes through an `AsyncPipe`.\nThat means the property returns either a `Promise` or an `Observable`.",
+ "translation": "注意该组件的 `quote` 属性的值是通过 `AsyncPipe` 传进来的。\n这意味着该属性或者返回 `Promise` 或者返回 `Observable`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In this example, the `TwainComponent.getQuote()` method tells you that \nthe `quote` property returns an `Observable`.",
+ "translation": "在这个例子中,`TwainComponent.getQuote()` 方法告诉你 `quote` 方法返回的是 `Observable`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `TwainComponent` gets quotes from an injected `TwainService`.\nThe component starts the returned `Observable` with a placeholder value (`'...'`),\nbefore the service can returns its first quote.",
+ "translation": "`TwainComponent` 会从一个注入进来的 `TwainService` 来获取这些引文。\n在服务返回第一条引文之前,该组件会先返回一个占位值(`'...'`)的 `Observable`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `catchError` intercepts service errors, prepares an error message,\nand returns the placeholder value on the success channel.\nIt must wait a tick to set the `errorMessage` \nin order to avoid updating that message twice in the same change detection cycle.",
+ "translation": "`catchError` 会拦截服务中的错误,准备错误信息,并在成功分支中返回占位值。\n它必须等一拍(tick)才能设置 `errorMessage`,以免在同一个变更检测周期中两次修改这个消息而导致报错。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "These are all features you'll want to test.",
+ "translation": "这就是你要测试的全部特性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Testing with a spy",
+ "translation": "#### 使用间谍(Spy)进行测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "When testing a component, only the service's public API should matter.\nIn general, tests themselves should not make calls to remote servers.\nThey should emulate such calls. The setup in this `app/twain/twain.component.spec.ts` shows one way to do that:",
+ "translation": "当测试组件时,只应该关心服务的公共 API。\n通常来说,测试不应该自己向远端服务器发起调用。\n它们应该对这些调用进行仿真。`app/twain/twain.component.spec.ts` 中的准备代码展示了实现方式之一:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Focus on the spy.",
+ "translation": "重点看这个间谍对象(spy)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The spy is designed such that any call to `getQuote` receives an Observable with a test quote.\nUnlike the real `getQuote()` method, this spy bypasses the server\nand returns a synchronous Observable whose value is available immediately.",
+ "translation": "这个间谍的设计是:任何对 `getQuote` 的调用都会收到一个包含测试引文的可观察对象。\n和真正的 `getQuote()` 方法不同,这个间谍跳过了服务器,直接返回了一个能立即解析出值的同步型可观察对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You can write many useful tests with this spy, even though its `Observable` is synchronous.",
+ "translation": "虽然它的 `Observable` 是同步的,不过你仍然可以使用这个间谍对象写出很多有用的测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Synchronous tests",
+ "translation": "#### 同步测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A key advantage of a synchronous `Observable` is that \nyou can often turn asynchronous processes into synchronous tests.",
+ "translation": "同步 `Observable` 的一大优点就是你可以把那些异步的流程转换成同步测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Because the spy result returns synchronously, the `getQuote()` method updates\nthe message on screen immediately _after_\nthe first change detection cycle during which Angular calls `ngOnInit`.",
+ "translation": "因为间谍对象的结果是同步返回的,所以 `getQuote()` 方法会在 Angular 调用 `ngOnInit` 时触发的首次变更检测周期后立即修改屏幕上的消息。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You're not so lucky when testing the error path.\nAlthough the service spy will return an error synchronously,\nthe component method calls `setTimeout()`.\nThe test must wait at least one full turn of the JavaScript engine before the\nvalue becomes available. The test must become _asynchronous_.",
+ "translation": "但测试出错路径的时候就没这么幸运了。\n虽然该服务的间谍也会返回一个同步的错误对象,但是组件的那个方法中调用了 `setTimeout()`。\n这个测试必须至少等待 JavaScript 引擎的一个周期,那个值才会变成可用状态。因此这个测试变成了*异步的*。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Async test with _fakeAsync()_",
+ "translation": "#### 使用 `fakeAsync()` 进行异步测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The following test confirms the expected behavior when the service returns an `ErrorObservable`.",
+ "translation": "下列测试用于确保当服务返回 `ErrorObservable` 的时候也能有符合预期的行为。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Note that the `it()` function receives an argument of the following form.",
+ "translation": "注意这个 `it()` 函数接收了一个如下形式的参数。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `fakeAsync` function enables a linear coding style by running the test body in a special _fakeAsync test zone_.\nThe test body appears to be synchronous.\nThere is no nested syntax (like a `Promise.then()`) to disrupt the flow of control.",
+ "translation": "`fakeAsync` 函数通过在一个特殊的*`fakeAsync`测试区域(zone)*中运行测试体来启用线性代码风格。\n测试体看上去是同步的。\n这里没有嵌套式语法(如`Promise.then()`)来打断控制流。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### The _tick()_ function",
+ "translation": "#### `tick()` 函数",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You do have to call `tick()` to advance the (virtual) clock.",
+ "translation": "你必须调用 `tick()` 函数来向前推动(虚拟)时钟。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Calling `tick()` simulates the passage of time until all pending asynchronous activities finish.\nIn this case, it waits for the error handler's `setTimeout()`;",
+ "translation": "调用 `tick()` 会模拟时光的流逝,直到所有未决的异步活动都结束为止。\n在这个例子中,它会等待错误处理器中的 `setTimeout()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `tick` function is one of the Angular testing utilities that you import with `TestBed`.\nIt's a companion to `fakeAsync` and you can only call it within a `fakeAsync` body.",
+ "translation": "`tick` 函数是你通过 `TestBed` 中引入的 Angular 测试工具集之一。\n它总是和 `fakeAsync` 一起使用,你也只能在 `fakeAsync` 的函数体中调用它。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Async observables",
+ "translation": "#### 异步的可观察对象",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You might be satisfied with the test coverage of these tests.",
+ "translation": "你可能对这些测试的覆盖率已经很满足了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "But you might be troubled by the fact that the real service doesn't quite behave this way.\nThe real service sends requests to a remote server.\nA server takes time to respond and the response certainly won't be available immediately\nas in the previous two tests.",
+ "translation": "不过你可能会因为真实的服务没有按这种方式工作而困扰。\n真实的服务器会把请求发送给远端服务器。\n服务需要花一些时间来作出响应,它的响应当然也不会真的像前面两个测试中那样立即可用。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Your tests will reflect the real world more faithfully if you return an _asynchronous_ observable\nfrom the `getQuote()` spy like this.",
+ "translation": "如果你在 `getQuote()` 间谍中返回一个*异步*可观察对象,那它就能更忠诚的反映出真实的世界了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Async observable helpers",
+ "translation": "#### 可观察对象的异步助手",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The async observable was produced by an `asyncData` helper\nThe `asyncData` helper is a utility function that you'll have to write yourself.\nOr you can copy this one from the sample code.",
+ "translation": "这个异步的可观察对象是用 `asyncData` 辅助函数生成的。\n`asyncData` 助手是一个工具函数,你可以自己写一个,也可以从下面的范例代码中复制一份。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This helper's observable emits the `data` value in the next turn of the JavaScript engine.",
+ "translation": "这个辅助函数的可观察对象会在 JavaScript 引擎的下一个工作周期中发出 `data` 的值。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "[RxJS `defer()`](http://reactivex.io/documentation/operators/defer.html) returns an observable.\nIt takes a factory function that returns either a promise or an observable.\nWhen something subscribes to _defer_'s observable,\nit adds the subscriber to a new observable created with that factory.",
+ "translation": "[RxJS `defer()`](http://reactivex.io/documentation/operators/defer.html) (延期)返回一个可观察对象。\n它获取一个工厂函数,这个工厂函数或者返回 Promise 或者返回 Observable。\n当有人订阅了这个 `defer` 的可观察对象时,它就会把这个订阅者添加到由那个工厂函数创建的新的可观察对象中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "RxJS `defer()` transform the `Promise.resolve()` into a new observable that, \nlike `HttpClient`, emits once and completes.\nSubscribers will be unsubscribed after they receive the data value.",
+ "translation": "RxJS `defer()` 会把 `Promise.resolve()` 转换成一个新的可观察对象,然后像 `HttpClient` 那样的发出一个值,然后结束。\n订阅者将会在接收到这个数据值之后自动被取消订阅。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "There's a similar helper for producing an async error.",
+ "translation": "下面是一个类似的用于产生异步错误的辅助函数。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### More async tests",
+ "translation": "#### 更多异步测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Now that the `getQuote()` spy is returning async observables,\nmost of your tests will have to be async as well.",
+ "translation": "现在,`getQuote()` 间谍会返回一个异步的可观察对象,你的大多数测试也同样要变成异步的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's a `fakeAsync()` test that demonstrates the data flow you'd expect \nin the real world.",
+ "translation": "下面这个 `fakeAsync()` 测试演示了你所期待的和真实世界中一样的数据流。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Notice that the quote element displays the placeholder value (`'...'`) after `ngOnInit()`.\nThe first quote hasn't arrived yet.",
+ "translation": "注意,这个 `` 元素应该在 `ngOnInit()` 之后显示占位值(`'...'`),\n但第一个引文却没有出现。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "To flush the first quote from the observable, you call `tick()`.\nThen call `detectChanges()` to tell Angular to update the screen.",
+ "translation": "要刷出可观察对象中的第一个引文,你就要先调用 `tick()`,然后调用 `detectChanges()` 来要求 Angular 刷新屏幕。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Then you can assert that the quote element displays the expected text.",
+ "translation": "然后你就可以断言这个 `` 元素应该显示所预期的文字了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Async test with _async()_",
+ "translation": "#### 使用 `async()` 进行异步测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `fakeAsync()` utility function has a few limitations.\nIn particular, it won't work if the test body makes an `XHR` call.",
+ "translation": "`fakeAsync()` 工具函数有一些限制。\n特别是,如果测试中发起了 `XHR` 调用,它就没用了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "`XHR` calls within a test are rare so you can generally stick with `fakeAsync()`.\nBut if you ever do need to call `XHR`, you'll want to know about `async()`.",
+ "translation": "测试中的 `XHR` 调用比较罕见,所以你通常会使用 `fakeAsync()`。\n不过你可能迟早会需要调用 `XHR`,那就来了解一些 `async()` 的知识吧。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `TestBed.compileComponents()` method (see [below](#compile-components)) calls `XHR`\nto read external template and css files during \"just-in-time\" compilation.\nWrite tests that call `compileComponents()` with the `async()` utility.",
+ "translation": "`TestBed.compileComponents()` 方法(参见[稍后](#compile-components))就会在 JIT 编译期间调用 `XHR` 来读取外部模板和 CSS 文件。\n如果写调用了 `compileComponents()` 的测试,就要用到 `async()` 工具函数了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's the previous `fakeAsync()` test, re-written with the `async()` utility.",
+ "translation": "下面是用 `async()` 工具函数重写的以前的 `fakeAsync()` 测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `async()` utility hides some asynchronous boilerplate by arranging for the tester's code \nto run in a special _async test zone_.\nYou don't have to pass Jasmine's `done()` into the test and call `done()` \nin promise or observable callbacks.",
+ "translation": "`async()` 工具函数通过把测试人员的代码放进在一个特殊的*async 测试区域*中,节省了一些用于异步调用的样板代码。\n你不必把 Jasmine 的 `done()` 传给这个测试,并在承诺(Promise)或可观察对象的回调中调用 `done()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "But the test's asynchronous nature is revealed by the call to `fixture.whenStable()`,\nwhich breaks the linear flow of control.",
+ "translation": "但是对 `fixture.whenStable()` 的调用揭示了该测试的异步本性,它将会打破线性的控制流。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test must wait for the `getQuote()` observable to emit the next quote.\nInstead of calling `tick()`, it calls `fixture.whenStable()`.",
+ "translation": "该测试必须等待 `getQuote()` 的可观察对象发出下一条引言。\n它不再调用 `tick()`,而是调用 `fixture.whenStable()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `fixture.whenStable()` returns a promise that resolves when the JavaScript engine's\ntask queue becomes empty. \nIn this example, the task queue becomes empty when the observable emits the first quote.",
+ "translation": "`fixture.whenStable()` 返回一个承诺,这个承诺会在 JavaScript 引擎的任务队列变为空白时被解析。\n在这个例子中,一旦这个可观察对象发出了第一条引言,这个任务队列就会变为空。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test resumes within the promise callback, which calls `detectChanges()` to \nupdate the quote element with the expected text.",
+ "translation": "该测试在这个承诺的回调中继续执行,它会调用 `detectChanges()` 来用预期的文本内容修改 `` 元素。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "While the `async` and `fakeAsync` functions greatly\nsimplify Angular asynchronous testing,\nyou can still fall back to the traditional technique\nand pass `it` a function that takes a\n[`done` callback](http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support).",
+ "translation": "虽然 `async` 和 `fakeAsync` 函数极大地简化了 Angular 的异步测试,不过你仍然可以回退到传统的技术中。\n也就是说给 `it` 额外传入一个函数型参数,这个函数接受一个 [`done` 回调](http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Now you are responsible for chaining promises, handling errors, and calling `done()` at the appropriate moments.",
+ "translation": "现在,你就要负责对 Promise 进行链接、处理错误,并在适当的时机调用 `done()` 了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Writing test functions with `done()`, is more cumbersome than `async`and `fakeAsync`.\nBut it is occasionally necessary.\nFor example, you can't call `async` or `fakeAsync` when testing\ncode that involves the `intervalTimer()` or the RxJS `delay()` operator.",
+ "translation": "写带有 `done()` 的测试函数会比 `async` 和 `fakeAsync` 方式更加冗长。\n不过有些时候它是必要的。\n比如,你不能在那些涉及到 `intervalTimer()` 或 RxJS 的 `delay()` 操作符时调用 `async` 或 `fakeAsync` 函数。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here are two mover versions of the previous test, written with `done()`.\nThe first one subscribes to the `Observable` exposed to the template by the component's `quote` property.",
+ "translation": "下面是对前面的测试用 `done()` 重写后的两个版本。\n第一个会订阅由组件的 `quote` 属性暴露给模板的那个 `Observable`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The RxJS `last()` operator emits the observable's last value before completing, which will be the test quote.\nThe `subscribe` callback calls `detectChanges()` to \nupdate the quote element with the test quote, in the same manner as the earlier tests.",
+ "translation": "RxJS 的 `last()` 操作符会在结束之前发出这个可观察对象的最后一个值,也就是那条测试引文。\n`subscribe` 回调中会像以前一样调用 `detectChanges()` 用这条测试引文更新 `` 元素。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In some tests, you're more interested in how an injected service method was called and what values it returned,\nthan what appears on screen.",
+ "translation": "有些测试中,相对于在屏幕上展示了什么,你可能会更关心所注入服务的某个方法是如何被调用的,以及它的返回值是什么。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A service spy, such as the `qetQuote()` spy of the fake `TwainService`,\ncan give you that information and make assertions about the state of the view.",
+ "translation": "服务的间谍,比如假冒服务 `TwainService` 的 `getQuote()` 间谍,可以给你那些信息,并且对视图的状态做出断言。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Component marble tests",
+ "translation": "### 组件的宝石测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The previous `TwainComponent` tests simulated an asynchronous observable response\nfrom the `TwainService` with the `asyncData` and `asyncError` utilities.",
+ "translation": "前面的 `TwainComponent` 测试中使用 `TwainService` 中的 `asyncData` 和 `asyncError` 工具函数仿真了可观察对象的异步响应。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "These are short, simple functions that you can write yourself.\nUnfortunately, they're too simple for many common scenarios.\nAn observable often emits multiple times, perhaps after a significant delay.\nA component may coordinate multiple observables\nwith overlapping sequences of values and errors.",
+ "translation": "那些都是你自己写的简短函数。\n很不幸,它们对于很多常见场景来说都太过简单了。\n可观察对象通常会发出很多次值,还可能会在显著的延迟之后。\n组件可能要协调多个由正常值和错误值组成的重叠序列的可观察对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "**RxJS marble testing** is a great way to test observable scenarios,\nboth simple and complex.\nYou've likely seen the [marble diagrams](http://rxmarbles.com/)\nthat illustrate how observables work.\nMarble testing uses a similar marble language to\nspecify the observable streams and expectations in your tests.",
+ "translation": "**RxJS 的宝石测试**是测试各种可观察对象场景的最佳方式 —— 无论简单还是复杂。\n你可以看看[宝石图](http://rxmarbles.com/),它揭示了可观察对象的工作原理。\n宝石测试使用类似的宝石语言来在你的测试中指定可观察对象流和对它们的期待。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The following examples revisit two of the `TwainComponent` tests\nwith marble testing.",
+ "translation": "下面的例子使用宝石测试重写了 `TwainComponent` 的两个测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Start by installing the `jasmine-marbles` npm package.\nThen import the symbols you need.",
+ "translation": "首先安装 `jasmine-marbles` 这个 npm 包,然后倒入所需的符号。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's the complete test for getting a quote:",
+ "translation": "下面是对获取引文功能的完整测试:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Notice that the Jasmine test is synchronous. There's no `fakeAsync()`.\nMarble testing uses a test scheduler to simulate the passage of time\nin a synchronous test.",
+ "translation": "注意,这个 Jasmine 测试是同步的。没有调用 `fakeAsync()`。\n宝石测试使用了一个测试调度程序来用同步的方式模拟时间的流逝。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The beauty of marble testing is in the visual definition of the observable streams.\nThis test defines a [_cold_ observable](#cold-observable) that waits \nthree [frames](#marble-frame) (`---`),\nemits a value (`x`), and completes (`|`).\nIn the second argument you map the value marker (`x`) to the emitted value (`testQuote`).",
+ "translation": "宝石测试的美妙之处在于它给出了可观察对象流的可视化定义。\n这个测试定义了一个[*冷的*可观察对象](#cold-observable),它等待三[帧](#marble-frame) (`---`),然后发出一个值(`x`),然后结束(`|`)。\n在第二个参数中,你把值标记(`x`)换成了实际发出的值(`testQuote`)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The marble library constructs the corresponding observable, which the\ntest sets as the `getQuote` spy's return value.",
+ "translation": "这个宝石库会构造出相应的可观察对象,测试代码会把它当做 `getQuote` 间谍的返回值。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "When you're ready to activate the marble observables,\nyou tell the `TestScheduler` to _flush_ its queue of prepared tasks like this.",
+ "translation": "当你已经准备好激活这个宝石库构造出的可观察对象时,只要让 `TestScheduler` 去*刷新*准备好的任务队列就可以了。代码如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This step serves a purpose analogous to `tick()` and `whenStable()` in the\nearlier `fakeAsync()` and `async()` examples.\nThe balance of the test is the same as those examples.",
+ "translation": "这个步骤的目的类似于前面的 `fakeAsync()` 和 `async()` 范例中的 `tick()` 和 `whenStable()`。\n这种测试的权衡方式也和那些例子中是一样的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Marble error testing",
+ "translation": "#### 宝石错误测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's the marble testing version of the `getQuote()` error test.",
+ "translation": "下面是 `getQuote()` 错误测试的宝石测试版本。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "It's still an async test, calling `fakeAsync()` and `tick()`, because the component itself\ncalls `setTimeout()` when processing errors.",
+ "translation": "它仍然是异步测试,要调用 `fakeAsync()` 和 `tick()`,这是因为组件自身在处理错误的时候调用 `setTimeout()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Look at the marble observable definition.",
+ "translation": "看看宝石库生成的可观察对象的定义。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This is a _cold_ observable that waits three frames and then emits an error,\nThe hash (`#`) indicates the timing of the error that is specified in the third argument.\nThe second argument is null because the observable never emits a value.",
+ "translation": "它是一个*冷的*可观察对象,它等待三帧,然后发出一个错误。\n井号(`#`)标记出了发出错误的时间点,这个错误是在第三个参数中指定的。\n第二个参数是空的,因为这个可观察对象永远不会发出正常值。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Learn about marble testing",
+ "translation": "#### 深入学习宝石测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A _marble frame_ is a virtual unit of testing time.\nEach symbol (`-`, `x`, `|`, `#`) marks the passing of one frame.",
+ "translation": "*宝石帧*是测试时序中的虚拟单元。\n每个符号(`-`,`x`,`|`,`#`)都表示一帧过去了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A _cold_ observable doesn't produce values until you subscribe to it.\nMost of your application observables are cold.\nAll [_HttpClient_](guide/http) methods return cold observables.",
+ "translation": "*冷的*可观察对象不会生成值,除非你订阅它。\n应用中的大多数可观察对象都是冷的。\n所有 [`HttpClient`](guide/http)的方法返回的都是冷的可观察对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A _hot_ observable is already producing values _before_ you subscribe to it.\nThe [_Router.events_](api/router/Router#events) observable, \nwhich reports router activity, is a _hot_ observable.",
+ "translation": "*热的*可观察对象在你订阅它之前就会生成值。\n[`Router.events`](api/router/Router#events) 可观察对象会主动汇报路由器的活动,它就是个*热的*可观察对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "RxJS marble testing is a rich subject, beyond the scope of this guide.\nLearn about it on the web, starting with the \n[official documentation](https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md).",
+ "translation": "RxJS 的宝石测试是一个内容丰富的主题,超出了本章的范围。\n要想在网络上进一步学习它,可以从 [official documentation](https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md) 开始。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Component with inputs and outputs",
+ "translation": "### 带有输入输出参数的组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "A component with inputs and outputs typically appears inside the view template of a host component.\nThe host uses a property binding to set the input property and an event binding to\nlisten to events raised by the output property.",
"translation": "带有导入和导出的组件通常出现在宿主组件的视图模板中。\n宿主使用属性绑定来设置输入属性,使用事件绑定来监听输出属性触发的事件。",
@@ -25186,7 +28441,7 @@
},
{
"original": "The `DashboardHeroComponent` appears in an `*ngFor` repeater, which sets each component's `hero` input property\nto the looping value and listens for the component's `selected` event.",
- "translation": "`DashboardHeroComponent`在`*ngFor`循环中出现,设置每个组件的`hero`input属性到迭代的值,并监听组件的`selected`事件。",
+ "translation": "`DashboardHeroComponent`在`*ngFor`循环中出现,把每个组件的 `hero` input 属性设置为迭代的值,并监听组件的`selected`事件。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25224,19 +28479,104 @@
"translation": "当前的任务是测试`DashboardHeroComponent`组件,而非`DashbaordComponent`,所以无需做不必要的努力。\n让我们尝试第二和第三种方案。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### Test _DashboardHeroComponent_ stand-alone",
+ "translation": "#### 单独测试 `DashboardHeroComponent`",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's the meat of the spec file setup.",
+ "translation": "下面是 spec 文件的准备语句中的重点部分。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Note how the setup code assigns a test hero (`expectedHero`) to the component's `hero` property,\nemulating the way the `DashboardComponent` would set it \nvia the property binding in its repeater.",
"translation": "注意代码是如何将模拟英雄(`expectedHero`)赋值给组件的`hero`属性的,模拟了`DashbaordComponent`在它的迭代器中通过属性绑定的赋值方式。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The following test verifies that the hero name is propagated to the template via a binding.",
+ "translation": "下面的测试会验证英雄的名字已经通过绑定的方式传播到模板中了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Because the [template](#dashboard-hero-component) passes the hero name through the Angular `UpperCasePipe`,\nthe test must match the element value with the upper-cased name.",
+ "translation": "因为[模板](#dashboard-hero-component)通过 Angular 的 `UpperCasePipe` 传入了英雄的名字,所以这个测试必须匹配该元素的值中包含了大写形式的名字。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This small test demonstrates how Angular tests can verify a component's visual\nrepresentation—something not possible with\n[component class tests](#component-class-testing)—at\nlow cost and without resorting to much slower and more complicated end-to-end tests.",
+ "translation": "这个小测试示范了 Angular 的测试如何以较低的成本验证组件的视觉表现(它们不能通过[组件类测试](#component-class-testing)进行验证)。\n而不用借助那些更慢、更复杂的端到端测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Clicking",
+ "translation": "#### 点击",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Clicking the hero should raise a `selected` event that \nthe host component (`DashboardComponent` presumably) can hear:",
+ "translation": "点击这个英雄将会发出一个 `selected` 事件,而宿主元素(可能是`DashboardComponent`)可能会听到它:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The component's `selected` property returns an `EventEmitter`, \nwhich looks like an RxJS synchronous `Observable` to consumers. \nThe test subscribes to it _explicitly_ just as the host component does _implicitly_.",
+ "translation": "该组件的 `selected` 属性返回一个 `EventEmitter`,对消费者来说它和 RxJS 的同步 `Observable` 很像。\n该测试会*显式*订阅它,而宿主组件会*隐式*订阅它。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "If the component behaves as expected, clicking the hero's element\nshould tell the component's `selected` property to emit the `hero` object.",
+ "translation": "如果该组件的行为符合预期,点击英雄所在的元素就会告诉组件的 `selected` 属性发出这个 `hero` 对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test detects that event through its subscription to `selected`.",
+ "translation": "这个测试会通过订阅 `selected` 来检测是否确实如此。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `heroDe` in the previous test is a `DebugElement` that represents the hero ``.",
+ "translation": "前面测试中的 `heroDe` 是一个指向英雄条目 `
` 的 `DebugElement`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "It has Angular properties and methods that abstract interaction with the native element.\nThis test calls the `DebugElement.triggerEventHandler` with the \"click\" event name.\nThe \"click\" event binding responds by calling `DashboardHeroComponent.click()`.",
+ "translation": "它有一些用于抽象与原生元素交互的 Angular 属性和方法。\n这个测试会使用事件名称 `click` 来调用 `DebugElement.triggerEventHandler`。\n`click` 的事件绑定到了 `DashboardHeroComponent.click()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The Angular `DebugElement.triggerEventHandler` can raise _any data-bound event_ by its _event name_.\nThe second parameter is the event object passed to the handler.",
"translation": "Angular的`DebugElement.triggerEventHandler`可以用**事件的名字**触发**任何数据绑定事件**。\n第二个参数是传递给事件处理器的事件对象。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The test triggered a \"click\" event with a `null` event object.",
+ "translation": "该测试使用事件对象 `null` 触发了一次 `click` 事件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The test assumes (correctly in this case) that the runtime\nevent handler—the component's `click()` method—doesn't\ncare about the event object.",
- "translation": "测试程序假设(在这里应该这样)运行时间的事件处理器——组件的`click()`方法——不关心事件对象。",
+ "translation": "测试程序假设(在这里应该这样)运行时间的事件处理器(组件的`click()`方法)不关心事件对象。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Other handlers are less forgiving. For example, the `RouterLink`\ndirective expects an object with a `button` property\nthat identifies which mouse button (if any) was pressed during the click.\nThe `RouterLink` directive throws an error if the event object is missing.",
+ "translation": "其它处理器的要求比较严格。比如,`RouterLink` 指令期望一个带有 `button` 属性的对象,该属性用于指出点击时按下的是哪个鼠标按钮。\n如果不给出这个事件对象,`RouterLink` 指令就会抛出一个错误。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Click the element",
+ "translation": "#### 点击该元素",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The following test alternative calls the native element's own `click()` method,\nwhich is perfectly fine for _this component_.",
+ "translation": "下面这个测试改为调用原生元素自己的 `click()` 方法,它对于*这个组件*来说相当完美。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### _click()_ helper",
+ "translation": "#### _click()_ 辅助函数",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25244,6 +28584,11 @@
"translation": "点击按钮、链接或者任意HTML元素是很常见的测试任务。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Make that consistent and easy by encapsulating the _click-triggering_ process \nin a helper such as the `click()` function below:",
+ "translation": "把*点击事件*的处理过程包装到如下的 `click()` 辅助函数中,可以让这项任务更一致、更简单:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The first parameter is the _element-to-click_. If you wish, you can pass a\ncustom event object as the second parameter. The default is a (partial)\n
left-button mouse event object \naccepted by many handlers including the `RouterLink` directive.",
"translation": "第一个参数是**用来点击的元素**。如果你愿意,可以将自定义的事件对象传递给第二个参数。\n默认的是(局部的)
鼠标左键事件对象 ,\n它被许多事件处理器接受,包括`RouterLink`指令。",
@@ -25254,6 +28599,56 @@
"translation": "`click()`辅助函数**不是**Angular测试工具之一。\n它是在**本章的例子代码**中定义的函数方法,被所有测试例子所用。\n如果你喜欢它,将它添加到你自己的辅助函数集。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Here's the previous test, rewritten using the click helper.",
+ "translation": "下面是把前面的测试用 `click` 辅助函数重写后的版本。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Component inside a test host",
+ "translation": "### 位于测试宿主中的组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The previous tests played the role of the host `DashboardComponent` themselves.\nBut does the `DashboardHeroComponent` work correctly when properly data-bound to a host component?",
+ "translation": "前面的这些测试都是自己扮演宿主元素 `DashboardComponent` 的角色。\n但是当 `DashboardHeroComponent` 真的绑定到某个宿主元素时还能正常工作吗?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You could test with the actual `DashboardComponent`.\nBut doing so could require a lot of setup, \nespecially when its template features an `*ngFor` repeater, \nother components, layout HTML, additional bindings, \na constructor that injects multiple services, \nand it starts interacting with those services right away.",
+ "translation": "固然,你也可以测试真实的 `DashboardComponent`。\n但要想这么做需要做很多准备工作,特别是它的模板中使用了某些特性,如 `*ngFor`、\n其它组件、布局 HTML、附加绑定、注入了多个服务的构造函数、如何用正确的方式与那些服务交互等。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Imagine the effort to disable these distractions, just to prove a point \nthat can be made satisfactorily with a _test host_ like this one:",
+ "translation": "想出这么多需要努力排除的干扰,只是为了证明一点 —— 可以像这样造出一个令人满意的*测试宿主*:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This test host binds to `DashboardHeroComponent` as the `DashboardComponent` would \nbut without the noise of the `Router`, the `HeroService`, or the `*ngFor` repeater.",
+ "translation": "这个测试宿主像 `DashboardComponent` 那样绑定了 `DashboardHeroComponent`,但是没有 `Router`、\n没有 `HeroService`,也没有 `*ngFor`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test host sets the component's `hero` input property with its test hero.\nIt binds the component's `selected` event with its `onSelected` handler,\nwhich records the emitted hero in its `selectedHero` property.",
+ "translation": "这个测试宿主使用其测试用的英雄设置了组件的输入属性 `hero`。\n它使用 `onSelected` 事件处理器绑定了组件的 `selected` 事件,其中把事件中发出的英雄记录到了 `selectedHero` 属性中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Later, the tests will be able to easily check `selectedHero` to verify that the\n`DashboardHeroComponent.selected` event emitted the expected hero.",
+ "translation": "稍后,这个测试就可以轻松检查 `selectedHero` 以验证 `DashboardHeroComponent.selected` 事件确实发出了所期望的英雄。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The setup for the _test-host_ tests is similar to the setup for the stand-alone tests:",
+ "translation": "这个*测试宿主*中的准备代码和独立测试中的准备过程类似:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This testing module configuration shows three important differences:",
+ "translation": "这个测试模块的配置信息有三个重要的不同点:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "1. It _declares_ both the `DashboardHeroComponent` and the `TestHostComponent`.",
"translation": "它同时**声明**了`DashboardHeroComponent`和`TestHostComponent`。",
@@ -25264,6 +28659,11 @@
"translation": "它**创建**了`TestHostComponent`,而非`DashboardHeroComponent`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "1. The `TestHostComponent` sets the `DashboardHeroComponent.hero` with a binding.",
+ "translation": "`TestHostComponent` 通过绑定机制设置了 `DashboardHeroComponent.hero`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The `createComponent` returns a `fixture` that holds an instance of `TestHostComponent` instead of an instance of `DashboardHeroComponent`.",
"translation": "`createComponent`返回的`fixture`里有`TestHostComponent`实例,而非`DashboardHeroComponent`组件实例。",
@@ -25284,11 +28684,171 @@
"translation": "只有selected事件的测试不一样。它确保被选择的`DashboardHeroComponent`英雄确实通过事件绑定被传递到宿主组件。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "### Routing component",
+ "translation": "### 路由组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A _routing component_ is a component that tells the `Router` to navigate to another component.\nThe `DashboardComponent` is a _routing component_ because the user can\nnavigate to the `HeroDetailComponent` by clicking on one of the _hero buttons_ on the dashboard.",
+ "translation": "所谓*路由组件*就是指会要求 `Router` 导航到其它组件的组件。\n`DashboardComponent` 就是一个*路由组件*,因为用户可以通过点击仪表盘中的某个*英雄按钮*来导航到 `HeroDetailComponent`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Routing is pretty complicated.\nTesting the `DashboardComponent` seemed daunting in part because it involves the `Router`,\nwhich it injects together with the `HeroService`.",
+ "translation": "路由确实很复杂。\n测试 `DashboardComponent` 看上去有点令人生畏,因为它牵扯到和 `HeroService` 一起注入进来的 `Router`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Mocking the `HeroService` with a spy is a [familiar story](#component-with-async-service).\nBut the `Router` has a complicated API and is entwined with other services and application preconditions. Might it be difficult to mock?",
+ "translation": "使用间谍来 Mock `HeroService` 是一个[熟悉的故事](#component-with-async-service)。\n但是 `Router` 的 API 很复杂,并且与其它服务和应用的前置条件纠缠在一起。它应该很难进行 Mock 吧?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Fortunately, not in this case because the `DashboardComponent` isn't doing much with the `Router`",
+ "translation": "庆幸的是,在这个例子中不会,因为 `DashboardComponent` 并没有深度使用 `Router`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This is often the case with _routing components_.\nAs a rule you test the component, not the router,\nand care only if the component navigates with the right address under the given conditions.",
+ "translation": "这是*路由组件*中的通例。\n一般来说,你应该测试组件而不是路由器,应该只关心组件有没有根据给定的条件导航到正确的地址。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Providing a router spy for _this component_ test suite happens to be as easy\nas providing a `HeroService` spy.",
+ "translation": "为*这个组件*的测试套件提供路由器的间谍就像它提供 `HeroService` 的间谍一样简单。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The following test clicks the displayed hero and confirms that \n`Router.navigateByUrl` is called with the expected url.",
+ "translation": "下面这个测试会点击正在显示的英雄,并确认 `Router.navigateByUrl` 曾用所期待的 URL 调用过。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Routed components",
+ "translation": "### 路由目标组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A _routed component_ is the destination of a `Router` navigation.\nIt can be trickier to test, especially when the route to the component _includes parameters_.\nThe `HeroDetailComponent` is a _routed component_ that is the destination of such a route.",
+ "translation": "*路由目标组件*是指 `Router` 导航到的目标。\n它测试起来可能很复杂,特别是当路由到的这个组件*包含参数*的时候。\n`HeroDetailComponent` 就是一个*路由目标组件*,它是某个路由定义指向的目标。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "When a user clicks a _Dashboard_ hero, the `DashboardComponent` tells the `Router`\nto navigate to `heroes/:id`.\nThe `:id` is a route parameter whose value is the `id` of the hero to edit.",
+ "translation": "当用户点击*仪表盘*中的英雄时,`DashboardComponent` 会要求 `Router` 导航到 `heroes/:id`。\n`:id` 是一个路由参数,它的值就是所要编辑的英雄的 `id`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `Router` matches that URL to a route to the `HeroDetailComponent`.\nIt creates an `ActivatedRoute` object with the routing information and\ninjects it into a new instance of the `HeroDetailComponent`.",
+ "translation": "该 `Router` 会根据那个 URL 匹配到一个指向 `HeroDetailComponent` 的路由。\n它会创建一个带有路由信息的 `ActivatedRoute` 对象,并把它注入到一个 `HeroDetailComponent` 的新实例中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here's the `HeroDetailComponent` constructor:",
+ "translation": "下面是 `HeroDetailComponent` 的构造函数:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `HeroDetail` component needs the `id` parameter so it can fetch \nthe corresponding hero via the `HeroDetailService`.",
+ "translation": "`HeroDetailComponent` 组件需要一个 `id` 参数,以便通过 `HeroDetailService` 获取相应的英雄。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The component has to get the `id` from the `ActivatedRoute.paramMap` property\nwhich is an _Observable_.",
+ "translation": "该组件只能从 `ActivatedRoute.paramMap` 属性中获取这个 `id`,这个属性是一个 `Observable`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "It can't just reference the `id` property of the `ActivatedRoute.paramMap`.\nThe component has to _subscribe_ to the `ActivatedRoute.paramMap` observable and be prepared\nfor the `id` to change during its lifetime.",
+ "translation": "它不能仅仅引用 `ActivatedRoute.paramMap` 的 `id` 属性。\n该组件不得不*订阅* `ActivatedRoute.paramMap` 这个可观察对象,要做好它在生命周期中随时会发生变化的准备。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The [Router](guide/router#route-parameters) guide covers `ActivatedRoute.paramMap` in more detail.",
+ "translation": "[路由与导航](guide/router#route-parameters)一章中详细讲解了 `ActivatedRoute.paramMap`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Tests can explore how the `HeroDetailComponent` responds to different `id` parameter values\nby manipulating the `ActivatedRoute` injected into the component's constructor.",
+ "translation": "通过操纵注入到组件构造函数中的这个 `ActivatedRoute`,测试可以探查 `HeroDetailComponent` 是如何对不同的 `id` 参数值做出响应的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You know how to spy on the `Router` and a data service.",
+ "translation": "你已经知道了如何给 `Router` 和数据服务安插间谍。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You'll take a different approach with `ActivatedRoute` because",
+ "translation": "不过对于 `ActivatedRoute`,你要采用另一种方式,因为:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* `paramMap` returns an `Observable` that can emit more than one value \nduring a test.",
+ "translation": "在测试期间,`paramMap` 会返回一个能发出多个值的 `Observable`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* You need the router helper function, `convertToParamMap()`, to create a `ParamMap`.",
+ "translation": "你需要路由器的辅助函数 `convertToParamMap()` 来创建 `ParamMap`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* Other _routed components_ tests need a test double for `ActivatedRoute`.",
+ "translation": "针对*路由目标组件*的其它测试需要一个 `ActivatedRoute` 的测试替身。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "These differences argue for a re-usable stub class.",
+ "translation": "这些差异表明你需要一个可复用的桩类(stub)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The following `ActivatedRouteStub` class serves as a test double for `ActivatedRoute`.",
+ "translation": "下面的 `ActivatedRouteStub` 类就是作为 `ActivatedRoute` 类的测试替身使用的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Consider placing such helpers in a `testing` folder sibling to the `app` folder.\nThis sample puts `ActivatedRouteStub` in `testing/activated-route-stub.ts`.",
+ "translation": "考虑把这类辅助函数放进一个紧邻 `app` 文件夹的 `testing` 文件夹。\n这个例子把 `ActivatedRouteStub` 放在了 `testing/activated-route-stub.ts` 中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Consider writing a more capable version of this stub class with\n the [_marble testing library_](#marble-testing).",
+ "translation": "可以考虑使用[宝石测试库](#marble-testing)来为此测试桩编写一个更强力的版本。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Testing with _ActivatedRouteStub_",
+ "translation": "#### 使用 `ActivatedRouteStub` 进行测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Here's a test demonstrating the component's behavior when the observed `id` refers to an existing hero:",
"translation": "下面的测试程序是演示组件在被观察的`id`指向现有英雄时的行为:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The `createComponent()` method and `page` object are discussed [below](#page-object).\nRely on your intuition for now.",
+ "translation": "`createComponent()` 方法和 `page` 对象会在[稍后](#page-object)进行讨论。\n不过目前,你只要凭直觉来理解就行了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "When the `id` cannot be found, the component should re-route to the `HeroListComponent`.",
+ "translation": "当找不到 `id` 的时候,组件应该重新路由到 `HeroListComponent`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test suite setup provided the same router spy [described above](#routing-component) which spies on the router without actually navigating.",
+ "translation": "测试套件的准备代码提供了一个和[前面](#routing-component)一样的路由器间谍,它会充当路由器的角色,而不用发起实际的导航。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This test expects the component to try to navigate to the `HeroListComponent`.",
+ "translation": "这个测试中会期待该组件尝试导航到 `HeroListComponent`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "While this app doesn't have a route to the `HeroDetailComponent` that omits the `id` parameter, it might add such a route someday.\nThe component should do something reasonable when there is no `id`.",
"translation": "虽然本应用没有在缺少`id`参数的时候,继续导航到`HeroDetailComponent`的路由,但是,将来它可能会添加这样的路由。\n当没有`id`时,该组件应该作出合理的反应。",
@@ -25299,24 +28859,259 @@
"translation": "在本例中,组件应该创建和显示新英雄。\n新英雄的`id`为零,`name`为空。本测试程序确认组件是按照预期的这样做的:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "### Nested component tests",
+ "translation": "### 对嵌套组件的测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Component templates often have nested components, whose templates\nmay contain more components.",
+ "translation": "组件的模板中通常还会有嵌套组件,嵌套组件的模板还可能包含更多组件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The component tree can be very deep and, most of the time, the nested components\nplay no role in testing the component at the top of the tree.",
+ "translation": "这棵组件树可能非常深,并且大多数时候在测试这棵树顶部的组件时,这些嵌套的组件都无关紧要。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `AppComponent`, for example, displays a navigation bar with anchors and their `RouterLink` directives.",
+ "translation": "比如,`AppComponent` 会显示一个带有链接及其 `RouterLink` 指令的导航条。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "While the `AppComponent` _class_ is empty,\nyou may want to write unit tests to confirm that the links are wired properly \nto the `RouterLink` directives, perhaps for the reasons [explained below](#why-stubbed-routerlink-tests).",
+ "translation": "虽然 `AppComponent` *类*是空的,不过,由于[稍后解释的原因](#why-stubbed-routerlink-tests),你可能会希望写个单元测试来确认这些链接是否正确使用了 `RouterLink` 指令。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "To validate the links, you don't need the `Router` to navigate and you don't\nneed the `
` to mark where the `Router` inserts _routed components_.",
+ "translation": "要想验证这些链接,你不必用 `Router` 进行导航,也不必使用 `` 来指出 `Router` 应该把*路由目标组件*插入到什么地方。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `BannerComponent` and `WelcomeComponent` \n(indicated by `` and ``) are also irrelevant.",
+ "translation": "而 `BannerComponent` 和 `WelcomeComponent`(写作 `` 和 ``)也同样风马牛不相及。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Yet any test that creates the `AppComponent` in the DOM will also create instances of\nthese three components and, if you let that happen, \nyou'll have to configure the `TestBed` to create them.",
+ "translation": "然而,任何测试,只要能在 DOM 中创建 `AppComponent`,也就同样能创建这三个组件的实例。如果要创建它们,你就要配置 `TestBed`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "If you neglect to declare them, the Angular compiler won't recognize the \n``, ``, and `` tags in the `AppComponent` template\nand will throw an error.",
+ "translation": "如果你忘了声明它们,Angular 编译器就无法在 `AppComponent` 模板中识别出 ``、`` 和 `` 标记,并抛出一个错误。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "If you declare the real components, you'll also have to declare _their_ nested components\nand provide for _all_ services injected in _any_ component in the tree.",
+ "translation": "如果你声明的这些都是真实的组件,那么也同样要声明*它们*的嵌套组件,并要为这棵组件树中的*任何*组件提供要注入的*所有*服务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "That's too much effort just to answer a few simple questions about links.",
+ "translation": "如果只是想回答有关链接的一些简单问题,做这些显然就太多了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "This section describes two techniques for minimizing the setup.\nUse them, alone or in combination, to stay focused on the testing the primary component.",
+ "translation": "本节会讲减少此类准备工作的两项技术。\n单独使用或组合使用它们,可以让这些测试聚焦于要测试的主要组件上。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "##### Stubbing unneeded components",
+ "translation": "##### 对不需要的组件提供桩(stub)",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In the first technique, you create and declare stub versions of the components\nand directive that play little or no role in the tests.",
+ "translation": "这项技术中,你要为那些在测试中无关紧要的组件或指令创建和声明一些测试桩。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The stub selectors match the selectors for the corresponding real components.\nBut their templates and classes are empty.",
+ "translation": "这些测试桩的选择器要和其对应的真实组件一致,但其模板和类是空的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Then declare them in the `TestBed` configuration next to the\ncomponents, directives, and pipes that need to be real.",
+ "translation": "然后在 `TestBed` 的配置中那些真正有用的组件、指令、管道之后声明它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `AppComponent` is the test subject, so of course you declare the real version.",
+ "translation": "`AppComponent` 是该测试的主角,因此当然要用它的真实版本。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `RouterLinkDirectiveStub`, [described later](#routerlink), is a test version\nof the real `RouterLink` that helps with the link tests.",
+ "translation": "而 `RouterLinkDirectiveStub`([稍后讲解](#routerlink))是一个真实的 `RouterLink` 的测试版,它能帮你对链接进行测试。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The rest are stubs.",
+ "translation": "其它都是测试桩。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In the second approach, add `NO_ERRORS_SCHEMA` to the `TestBed.schemas` metadata.",
+ "translation": "第二种办法就是把 `NO_ERRORS_SCHEMA` 添加到 `TestBed.schemas` 的元数据中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `NO_ERRORS_SCHEMA` tells the Angular compiler to ignore unrecognized elements and attributes.",
+ "translation": "`NO_ERRORS_SCHEMA` 会要求 Angular 编译器忽略不认识的那些元素和属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The compiler will recognize the `` element and the `routerLink` attribute\nbecause you declared a corresponding `AppComponent` and `RouterLinkDirectiveStub`\nin the `TestBed` configuration.",
+ "translation": "编译器将会识别出 `` 元素和 `RouterLink` 属性,因为你在 `TestBed` 的配置中声明了相应的\n`AppComponent` 和 `RouterLinkDirectiveStub`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "But the compiler won't throw an error when it encounters ``, ``, or ``.\nIt simply renders them as empty tags and the browser ignores them.",
+ "translation": "但编译器在遇到 ``、`` 或 `` 时不会报错。\n它只会把它们渲染成空白标签,而浏览器会忽略这些标签。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You no longer need the stub components.",
+ "translation": "你不用再提供桩组件了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Use both techniques together",
+ "translation": "#### 同时使用这两项技术",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "These are techniques for _Shallow Component Testing_ , \nso-named because they reduce the visual surface of the component to just those elements\nin the component's template that matter for tests.",
+ "translation": "这些是进行*浅层*测试要用到的技术,之所以叫浅层测试是因为只包含本测试所关心的这个组件模板中的元素。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `NO_ERRORS_SCHEMA` approach is the easier of the two but don't overuse it.",
+ "translation": "`NO_ERRORS_SCHEMA` 方法在这两者中比较简单,但也不要过度使用它。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `NO_ERRORS_SCHEMA` also prevents the compiler from telling you about the missing \ncomponents and attributes that you omitted inadvertently or misspelled.\nYou could waste hours chasing phantom bugs that the compiler would have caught in an instant.",
+ "translation": "`NO_ERRORS_SCHEMA` 还会阻止编译器告诉你因为的疏忽或拼写错误而缺失的组件和属性。\n你如果人工找出这些 bug 可能要浪费几个小时,但编译器可以立即捕获它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The _stub component_ approach has another advantage.\nWhile the stubs in _this_ example were empty,\nyou could give them stripped-down templates and classes if your tests\nneed to interact with them in some way.",
+ "translation": "*桩组件*方式还有其它优点。\n虽然*这个*例子中的桩是空的,但你如果想要和它们用某种形式互动,也可以给它们一些裁剪过的模板和类。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In practice you will combine the two techniques in the same setup,\nas seen in this example.",
+ "translation": "在实践中,你可以在准备代码中组合使用这两种技术,例子如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The Angular compiler creates the `BannerComponentStub` for the `` element\nand applies the `RouterLinkStubDirective` to the anchors with the `routerLink` attribute,\nbut it ignores the `` and `` tags.",
+ "translation": "Angular 编译器会为 `` 元素创建 `BannerComponentStub`,并把 `RouterLinkStubDirective` 应用到带有 `routerLink` 属性的链接上,不过它会忽略 `` 和 `` 标签。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Components with _RouterLink_",
+ "translation": "### 带有 `RouterLink` 的组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The real `RouterLinkDirective` is quite complicated and entangled with other components\nand directives of the `RouterModule`.\nIt requires challenging setup to mock and use in tests.",
+ "translation": "真实的 `RouterLinkDirective` 太复杂了,而且与 `RouterModule` 中的其它组件和指令有着千丝万缕的联系。\n要在准备阶段 Mock 它以及在测试中使用它具有一定的挑战性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `RouterLinkDirectiveStub` in this sample code replaces the real directive\nwith an alternative version designed to validate the kind of anchor tag wiring\nseen in the `AppComponent` template.",
+ "translation": "这段范例代码中的 `RouterLinkDirectiveStub` 用一个代用品替换了真实的指令,这个代用品用来验证 `AppComponent` 中所用链接的类型。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The URL bound to the `[routerLink]` attribute flows in to the directive's `linkParams` property.",
+ "translation": "这个 URL 被绑定到了 `[routerLink]` 属性,它的值流入了该指令的 `linkParams` 属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `host` metadata property wires the click event of the host element \n(the `` anchor elements in `AppComponent`) to the stub directive's `onClick` method.",
+ "translation": "它的元数据中的 `host` 属性把宿主元素(即 `AppComponent` 中的 ` ` 元素)的 `click` 事件关联到了这个桩指令的 `onClick` 方法。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Clicking the anchor should trigger the `onClick()` method, \nwhich sets the stub's telltale `navigatedTo` property.\nTests inspect `navigatedTo` to confirm that clicking the anchor\nset the expected route definition.",
+ "translation": "点击这个链接应该触发 `onClick()` 方法,其中会设置该桩指令中的警示器属性 `navigatedTo`。\n测试中检查 `navigatedTo` 以确认点击该链接确实如预期的那样根据路由定义设置了该属性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Whether the router is configured properly to navigate with that route definition is a\nquestion for a separate set of tests.",
+ "translation": "路由器的配置是否正确和是否能按照那些路由定义进行导航,是测试中一组独立的问题。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### _By.directive_ and injected directives",
+ "translation": "#### `By.directive` 与注入的指令",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "A little more setup triggers the initial data binding and gets references to the navigation links:",
"translation": "再一步配置触发了数据绑定的初始化,获取导航链接的引用:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Three points of special interest:",
+ "translation": "有三点特别重要:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "1. You can locate the anchor elements with an attached directive using `By.directive`.",
+ "translation": "你可以使用 `By.directive` 来定位一个带附属指令的链接元素。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "1. The query returns `DebugElement` wrappers around the matching elements.",
+ "translation": "该查询返回包含了匹配元素的 `DebugElement` 包装器。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "1. Each `DebugElement` exposes a dependency injector with the\n specific instance of the directive attached to that element.",
+ "translation": "每个 `DebugElement` 都会导出该元素中的一个依赖注入器,其中带有指定的指令实例。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `AppComponent` links to validate are as follows:",
+ "translation": "`AppComponent` 中要验证的链接如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here are some tests that confirm those links are wired to the `routerLink` directives\nas expected:",
+ "translation": "下面这些测试用来确认那些链接是否如预期般连接到了 `RouterLink` 指令中:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The \"click\" test _in this example_ is misleading.\nIt tests the `RouterLinkDirectiveStub` rather than the _component_.\nThis is a common failing of directive stubs.",
+ "translation": "其实*这个例子中*的“click”测试误入歧途了。\n它测试的重点其实是 `RouterLinkDirectiveStub` ,而不是该组件。\n这是写桩指令时常见的错误。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "It has a legitimate purpose in this guide.\nIt demonstrates how to find a `RouterLink` element, click it, and inspect a result,\nwithout engaging the full router machinery.\nThis is a skill you may need to test a more sophisticated component, one that changes the display,\nre-calculates parameters, or re-arranges navigation options when the user clicks the link.",
"translation": "在本章中,它有存在的必要。\n它演示了如何在不涉及完整路由器机制的情况下,如何找到`RouterLink`元素、点击它并检查结果。\n要测试更复杂的组件,你可能需要具备这样的能力,能改变视图和重新计算参数,或者当用户点击链接时,有能力重新安排导航选项。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### What good are these tests?",
+ "translation": "#### 这些测试有什么优点?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Stubbed `RouterLink` tests can confirm that a component with links and an outlet is setup properly,\nthat the component has the links it should have, and that they are all pointing in the expected direction.\nThese tests do not concern whether the app will succeed in navigating to the target component when the user clicks a link.",
- "translation": "stub伪造的`RouterLink`测试可以确认带有链接和outlet的组件的设置的正确性,确认组件有应该有的链接,确认它们都指向了正确的方向。\n这些测试程序不关心用户点击链接时,应用是否会成功的导航到目标组件。",
+ "translation": "用 `RouterLink` 的桩指令进行测试可以确认带有链接和outlet的组件的设置的正确性,确认组件有应该有的链接,确认它们都指向了正确的方向。\n这些测试程序不关心用户点击链接时,也不关心应用是否会成功的导航到目标组件。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
"original": "Stubbing the RouterLink and RouterOutlet is the best option for such limited testing goals.\nRelying on the real router would make them brittle.\nThey could fail for reasons unrelated to the component.\nFor example, a navigation guard could prevent an unauthorized user from visiting the `HeroListComponent`.\nThat's not the fault of the `AppComponent` and no change to that component could cure the failed test.",
- "translation": "对于这样局限的测试目标,stub伪造RouterLink和RouterOutlet是最佳选择。\n依靠真正的路由器会让它们很脆弱。\n它们可能因为与组件无关的原因而失败。\n例如,一个导航守卫可能防止没有授权的用户访问`HeroListComponent`。\n这并不是`AppComponent`的过错,并且无论该组件怎么改变都无法修复这个失败的测试程序。",
+ "translation": "对于这些有限的测试目标,使用 RouterLink 桩指令和 RouterOutlet 桩组件 是最佳选择。\n依靠真正的路由器会让它们很脆弱。\n它们可能因为与组件无关的原因而失败。\n例如,一个导航守卫可能防止没有授权的用户访问`HeroListComponent`。\n这并不是`AppComponent`的过错,并且无论该组件怎么改变都无法修复这个失败的测试程序。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25326,7 +29121,12 @@
},
{
"original": "A future guide update will explain how to write such\ntests with the `RouterTestingModule`.",
- "translation": "未来本章的更新将介绍如何使用`RouterTestingModule`来编写这样的测试程序。",
+ "translation": "未来对本章的更新将介绍如何使用 `RouterTestingModule` 来编写这样的测试程序。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Use a _page_ object",
+ "translation": "### 使用页面(page)对象",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25334,11 +29134,56 @@
"translation": "`HeroDetailComponent`是带有标题、两个英雄字段和两个按钮的简单视图。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "But there's plenty of template complexity even in this simple form.",
+ "translation": "但即使是这么简单的表单,其模板中也涉及到不少复杂性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Tests that exercise the component need ...",
+ "translation": "这些供练习用的组件需要 ……",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* to wait until a hero arrives before elements appear in the DOM.",
+ "translation": "等获取到英雄之后才能让元素出现在 DOM 中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* a reference to the title text.",
+ "translation": "一个对标题文本的引用。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* a reference to the name input box to inspect and set it.",
+ "translation": "一个对 name 输入框的引用,以便对它进行探查和修改。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* references to the two buttons so they can click them.",
+ "translation": "引用两个按钮,以便点击它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "* spies for some of the component and router methods.",
+ "translation": "为组件和路由器的方法安插间谍。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Even a small form such as this one can produce a mess of tortured conditional setup and CSS element selection.",
"translation": "即使是像这样一个很小的表单,也能产生令人疯狂的错综复杂的条件设置和CSS元素选择。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Tame the complexity with a `Page` class that handles access to component properties\nand encapsulates the logic that sets them.",
+ "translation": "可以使用 `Page` 类来征服这种复杂性。`Page` 类可以处理对组件属性的访问,并对设置这些属性的逻辑进行封装。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here is such a `Page` class for the `hero-detail.component.spec.ts`",
+ "translation": "下面是一个供 `hero-detail.component.spec.ts` 使用的 `Page` 类",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Now the important hooks for component manipulation and inspection are neatly organized and accessible from an instance of `Page`.",
"translation": "现在,用来操作和检查组件的重要钩子都被井然有序的组织起来了,可以通过`page`实例来使用它们。",
@@ -25349,11 +29194,196 @@
"translation": "`createComponent`方法创建`page`,在`hero`到来时,自动填补空白。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The [_HeroDetailComponent_ tests](#tests-w-test-double) in an earlier section demonstrate how `createComponent` and `page`\nkeep the tests short and _on message_.\nThere are no distractions: no waiting for promises to resolve and no searching the DOM for element values to compare.",
+ "translation": "前面小节中的 [`HeroDetailComponent` 测试](#tests-w-test-double)示范了如何 `createComponent`,而 `page` 让这些测试保持简短而富有表达力。\n而且还不用分心:不用等待承诺被解析,不必在 DOM 中找出元素的值才能进行比较。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Here are a few more `HeroDetailComponent` tests to reinforce the point.",
+ "translation": "还有更多的 `HeroDetailComponent` 测试可以证明这一点。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Calling _compileComponents()_",
+ "translation": "### 调用 `compileComponents()`",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You can ignore this section if you _only_ run tests with the CLI `ng test` command\nbecause the CLI compiles the application before running the tests.",
+ "translation": "如果你*只想*使用 CLI 的 `ng test` 命令来运行测试,那么可以忽略这一节。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "If you run tests in a **non-CLI environment**, the tests may fail with a message like this one:",
+ "translation": "如果你在**非 CLI 环境**中运行测试,这些测试可能会报错,错误信息如下:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The root of the problem is at least one of the components involved in the test\nspecifies an external template or CSS file as \nthe following version of the `BannerComponent` does.",
+ "translation": "问题的根源在于这个测试中至少有一个组件引用了外部模板或外部 CSS 文件,就像下面这个 `BannerComponent` 所示:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test fails when the `TestBed` tries to create the component.",
+ "translation": "当 `TestBed` 视图创建组件时,这个测试失败了:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Recall that the app hasn't been compiled. \nSo when you call `createComponent()`, the `TestBed` compiles implicitly.",
+ "translation": "回想一下,这个应用从未编译过。\n所以当你调用 `createComponent()` 的时候,`TestBed` 就会进行隐式编译。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "That's not a problem when the source code is in memory.\nBut the `BannerComponent` requires external files\nthat the compile must read from the file system, \nan inherently _asynchronous_ operation.",
+ "translation": "当它的源码都在内存中的时候,这样做没问题。\n不过 `BannerComponent` 需要一些外部文件,编译时必须从文件系统中读取它,而这是一个天生的*异步*操作。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "If the `TestBed` were allowed to continue, the tests would run and fail mysteriously\nbefore the compiler could finished.",
+ "translation": "如果 `TestBed` 继续执行,这些测试就会继续运行,并在编译器完成这些异步工作之前导致莫名其妙的失败。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The preemptive error message tells you to compile explicitly with `compileComponents()`.",
+ "translation": "这些错误信息告诉你要使用 `compileComponents()` 进行显式的编译。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### _compileComponents()_ is async",
+ "translation": "#### `compileComponents()` 是异步的",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You must call `compileComponents()` within an asynchronous test function.",
+ "translation": "你必须在异步测试函数中调用 `compileComponents()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "If you neglect to make the test function async \n(e.g., forget to use `async()` as described below),\nyou'll see this error message",
+ "translation": "如果你忘了把测试函数标为异步的(比如忘了像稍后的代码中那样使用 `async()`),就会看到下列错误。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A typical approach is to divide the setup logic into two separate `beforeEach()` functions:",
+ "translation": "典型的做法是把准备逻辑拆成两个独立的 `beforeEach()` 函数:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "1. An async `beforeEach()` that compiles the components",
+ "translation": "异步的 `beforeEach()` 负责编译组件",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "1. A synchronous `beforeEach()` that performs the remaining setup.",
+ "translation": "同步的 `beforeEach()` 负责执行其余的准备代码。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "To follow this pattern, import the `async()` helper with the other testing symbols.",
+ "translation": "要想使用这种模式,就要和其它符号一起从测试库中导入 `async()` 辅助函数。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### The async _beforeEach_",
+ "translation": "#### 异步的 `beforeEach`",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Write the first async `beforeEach` like this.",
+ "translation": "像下面这样编写第一个异步的 `beforeEach`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `async()` helper function takes a parameterless function with the body of the setup.",
+ "translation": "`async()` 辅助函数接受一个无参函数,其内容是准备代码。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `TestBed.configureTestingModule()` method returns the `TestBed` class so you can chain\ncalls to other `TestBed` static methods such as `compileComponents()`.",
+ "translation": "`TestBed.configureTestingModule()` 方法返回 `TestBed` 类,所以你可以链式调用其它 `TestBed` 中的静态方法,比如 `compileComponents()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "In this example, the `BannerComponent` is the only component to compile.\nOther examples configure the testing module with multiple components\nand may import application modules that hold yet more components.\nAny of them could be require external files.",
+ "translation": "在这个例子中,`BannerComponent` 是仅有的待编译组件。\n其它例子中可能会使用多个组件来配置测试模块,并且可能引入某些具有其它组件的应用模块。\n它们中的任何一个都可能需要外部文件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `TestBed.compileComponents` method asynchronously compiles all components configured in the testing module.",
+ "translation": "`TestBed.compileComponents` 方法会异步编译测试模块中配置过的所有组件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Do not re-configure the `TestBed` after calling `compileComponents()`.",
+ "translation": "在调用了 `compileComponents()` 之后就不能再重新配置 `TestBed` 了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Calling `compileComponents()` closes the current `TestBed` instance to further configuration.\nYou cannot call any more `TestBed` configuration methods, not `configureTestingModule()`\nnor any of the `override...` methods. The `TestBed` throws an error if you try.",
+ "translation": "调用 `compileComponents()` 会关闭当前的 `TestBed` 实例,不再允许进行配置。\n你不能再调用任何 `TestBed` 中的配置方法,既不能调 `configureTestingModule()`,也不能调用任何 `override...` 方法。如果你试图这么做,`TestBed` 就会抛出错误。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Make `compileComponents()` the last step\nbefore calling `TestBed.createComponent()`.",
+ "translation": "确保 `compileComponents()` 是调用 `TestBed.createComponent()` 之前的最后一步。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### The synchronous _beforeEach_",
+ "translation": "#### 同步的 `beforeEach`",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The second, synchronous `beforeEach()` contains the remaining setup steps, \nwhich include creating the component and querying for elements to inspect.",
+ "translation": "第二个同步 `beforeEach()` 的例子包含剩下的准备步骤,\n包括创建组件和查询那些要检查的元素。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "You can count on the test runner to wait for the first asynchronous `beforeEach` to finish before calling the second.",
"translation": "测试运行器(runner)会先等待第一个异步`beforeEach`函数执行完再调用第二个。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### Consolidated setup",
+ "translation": "#### 整理过的准备代码",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "You can consolidate the two `beforeEach()` functions into a single, async `beforeEach()`.",
+ "translation": "你可以把这两个 `beforeEach()` 函数重整成一个异步的 `beforeEach()`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `compileComponents()` method returns a promise so you can perform the\nsynchronous setup tasks _after_ compilation by moving the synchronous code\ninto a `then(...)` callback.",
+ "translation": "`compileComponents()` 方法返回一个承诺,所以你可以通过把同步代码移到 `then(...)` 回调中,\n以便在编译完成*之后* 执行那些同步准备任务。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### _compileComponents()_ is harmless",
+ "translation": "#### `compileComponents()` 是无害的",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "There's no harm in calling `compileComponents()` when it's not required.",
+ "translation": "在不需要 `compileComponents()` 的时候调用它也不会有害处。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The component test file generated by the CLI calls `compileComponents()`\neven though it is never required when running `ng test`.",
+ "translation": "虽然在运行 `ng test` 时永远都不需要调用 `compileComponents()`,但 CLI 生成的组件测试文件还是会调用它。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The tests in this guide only call `compileComponents` when necessary.",
+ "translation": "但这篇指南中的这些测试只会在必要时才调用 `compileComponents`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Setup with module imports",
+ "translation": "### 准备模块的 `imports`",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Earlier component tests configured the testing module with a few `declarations` like this:",
"translation": "此前的组件测试程序使用了一些`declarations`来配置模块,就像这样:",
@@ -25399,16 +29429,66 @@
"translation": "一种方法是在测试模块中一一配置,就像这样:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Notice that the `beforeEach()` is asynchronous and calls `TestBed.compileComponents`\nbecause the `HeroDetailComponent` has an external template and css file.",
+ "translation": "注意,`beforeEach()` 是异步的,它调用 `TestBed.compileComponents` 是因为 `HeroDetailComponent` 有外部模板和 CSS 文件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "As explained in [_Calling compileComponents()_](#compile-components) above,\nthese tests could be run in a non-CLI environment\nwhere Angular would have to compile them in the browser.",
+ "translation": "如前面的[调用 `compileComponents()`](#compile-components) 中所解释的那样,这些测试可以运行在非 CLI 环境下,那里 Angular 并不会在浏览器中编译它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Import a shared module",
+ "translation": "#### 导入共享模块",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Because many app components need the `FormsModule` and the `TitleCasePipe`, the developer created\na `SharedModule` to combine these and other frequently requested parts.",
+ "translation": "因为很多应用组件都需要 `FormsModule` 和 `TitleCasePipe`,所以开发者创建了 `SharedModule` 来把它们及其它常用的部分组合在一起。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The test configuration can use the `SharedModule` too as seen in this alternative setup:",
+ "translation": "这些测试配置也可以使用 `SharedModule`,如下所示:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "It's a bit tighter and smaller, with fewer import statements (not shown).",
"translation": "它的导入声明少一些(未显示),稍微干净一些,小一些。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### Import a feature module",
+ "translation": "#### 导入特性模块",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `HeroDetailComponent` is part of the `HeroModule` [Feature Module](guide/feature-modules) that aggregates more of the interdependent pieces\nincluding the `SharedModule`.\nTry a test configuration that imports the `HeroModule` like this one:",
+ "translation": "`HeroDetailComponent` 是 `HeroModule` 这个[特性模块](guide/feature-modules)的一部分,它聚合了更多相互依赖的片段,包括 `SharedModule`。\n试试下面这个导入了 `HeroModule` 的测试配置:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "That's _really_ crisp. Only the _test doubles_ in the `providers` remain. Even the `HeroDetailComponent` declaration is gone.",
"translation": "这样特别清爽。只有`providers`里面的测试替身被保留。连`HeroDetailComponent`声明都消失了。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "In fact, if you try to declare it, Angular will throw an error because\n`HeroDetailComponent` is declared in both the `HeroModule` and the `DynamicTestModule`\ncreated by the `TestBed`.",
+ "translation": "事实上,如果你试图声明它,Angular 就会抛出一个错误,因为 `HeroDetailComponent` 同时声明在了 `HeroModule` 和`TestBed` 创建的 `DynamicTestModule` 中。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Importing the component's feature module can be the easiest way to configure tests\nwhen there are many mutual dependencies within the module and \nthe module is small, as feature modules tend to be.",
+ "translation": "如果模块中有很多共同依赖,并且该模块很小(这也是特性模块的应有形态),那么直接导入组件的特性模块可以成为配置这些测试的简易方式。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "### Override component providers",
+ "translation": "### 改写组件的服务提供商",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The `HeroDetailComponent` provides its own `HeroDetailService`.",
"translation": "`HeroDetailComponent`提供自己的`HeroDetailService`服务。",
@@ -25419,6 +29499,16 @@
"translation": "在`TestBed.configureTestingModule`的`providers`中stub伪造组件的`HeroDetailService`是不可行的。\n这些是**测试模块**的提供商,而非组件的。组件级别的供应商应该在**fixture级别**准备的依赖注入器。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Angular creates the component with its _own_ injector, which is a _child_ of the fixture injector.\nIt registers the component's providers (the `HeroDetailService` in this case) with the child injector.",
+ "translation": "Angular 会使用自己的注入器来创建这些组件,这个注入器是夹具的注入器的子注入器。\n它使用这个子注入器注册了该组件服务提供商(这里是 `HeroDetailService` )。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "A test cannot get to child injector services from the fixture injector.\nAnd `TestBed.configureTestingModule` can't configure them either.",
+ "translation": "测试没办法从测试夹具的注入器中获取子注入器中的服务,而 `TestBed.configureTestingModule` 也没法配置它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Angular has been creating new instances of the real `HeroDetailService` all along!",
"translation": "Angular始终都在创建真实`HeroDetailService`的实例。",
@@ -25434,6 +29524,11 @@
"translation": "幸运的是,`HeroDetailService`将远程数据访问的责任交给了注入进来的`HeroService`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The [previous test configuration](#feature-module-import) replaces the real `HeroService` with a `TestHeroService`\nthat intercepts server requests and fakes their responses.",
+ "translation": "[前面的测试配置](#feature-module-import)使用 `TestHeroService` 替换了真实的 `HeroService`,它拦截了发往服务器的请求,并伪造了服务器的响应。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "What if you aren't so lucky. What if faking the `HeroService` is hard?\nWhat if `HeroDetailService` makes its own server requests?",
"translation": "如果我们没有这么幸运怎么办?如果伪造`HeroService`很难怎么办?如果`HeroDetailService`自己发出服务器请求怎么办?",
@@ -25441,7 +29536,17 @@
},
{
"original": "The `TestBed.overrideComponent` method can replace the component's `providers` with easy-to-manage _test doubles_\nas seen in the following setup variation:",
- "translation": "`TestBed.overrideComponent`方法可以将组件的`providers`替换为容易管理的**测试替身**,参见下面的设置变化:",
+ "translation": "`TestBed.overrideComponent`方法可以将组件的`providers`替换为容易管理的**测试替身**,参见下面的变体准备代码:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Notice that `TestBed.configureTestingModule` no longer provides a (fake) `HeroService` because it's [not needed](#spy-stub).",
+ "translation": "注意,`TestBed.configureTestingModule` 不再提供(伪造的)`HeroService`,因为[并不需要](#spy-stub)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### The _overrideComponent_ method",
+ "translation": "#### `overrideComponent` 方法",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25449,6 +29554,11 @@
"translation": "注意这个`overrideComponent`方法。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "It takes two arguments: the component type to override (`HeroDetailComponent`) and an override metadata object.\nThe [overide metadata object](#metadata-override-object) is a generic defined as follows:",
+ "translation": "它接受两个参数:要改写的组件类(`HeroDetailComponent`),以及用于改写的元数据对象:",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "A metadata override object can either add-and-remove elements in metadata properties or completely reset those properties.\nThis example resets the component's `providers` metadata.",
"translation": "元数据重载对象可以添加和删除元数据属性的项目,也可以彻底重设这些属性。\n这个例子重新设置了组件的`providers`元数据。",
@@ -25459,6 +29569,11 @@
"translation": "这个类型参数,`T`,是你会传递给`@Component`装饰器的元数据的类型。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### Provide a _spy stub_ (_HeroDetailServiceSpy_)",
+ "translation": "#### 提供`间谍桩` (`HeroDetailServiceSpy`)",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "This example completely replaces the component's `providers` array with a new array containing a `HeroDetailServiceSpy`.",
"translation": "这个例子把组件的`providers`数组完全替换成了一个包含`HeroDetailServiceSpy`的新数组。",
@@ -25474,11 +29589,21 @@
"translation": "通过对该服务的方法进行刺探,`HeroDetailComponent`的关联测试将会对`HeroDetailService`是否被调用过进行断言。\n因此,这个桩类会把它的方法实现为刺探方法:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### The override tests",
+ "translation": "#### 改写测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Now the tests can control the component's hero directly by manipulating the spy-stub's `testHero`\nand confirm that service methods were called.",
"translation": "现在,测试程序可以通过操控stub的`testHero`,直接控制组件的英雄,并确保服务的方法被调用过。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### More overrides",
+ "translation": "#### 更多的改写",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The `TestBed.overrideComponent` method can be called multiple times for the same or different components.\nThe `TestBed` offers similar `overrideDirective`, `overrideModule`, and `overridePipe` methods\nfor digging into and replacing parts of these other classes.",
"translation": "`TestBed.overrideComponent`方法可以在相同或不同的组件中被反复调用。\n`TestBed`还提供了类似的`overrideDirective`、`overrideModule`和`overridePipe`方法,用来深入并重载这些其它类的部件。",
@@ -25489,6 +29614,11 @@
"translation": "自己探索这些选项和组合。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "## Attribute Directive Testing",
+ "translation": "## 属性型指令的测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "An _attribute directive_ modifies the behavior of an element, component or another directive.\nIts name reflects the way the directive is applied: as an attribute on a host element.",
"translation": "**属性指令**修改元素、组件和其它指令的行为。正如它们的名字所示,它们是作为宿主元素的属性来被使用的。",
@@ -25504,14 +29634,24 @@
"translation": "它的使用贯穿整个应用,也许最简单的使用在`AboutComponent`里:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Testing the specific use of the `HighlightDirective` within the `AboutComponent` requires only the\ntechniques explored above (in particular the [\"Shallow test\"](#nested-component-tests) approach).",
+ "translation": "要想在 `AboutComponent` 中测试 `HighlightDirective` 的具体用法,只要使用在[“浅层测试”](#nested-component-tests)部分用过的技术即可。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "However, testing a single use case is unlikely to explore the full range of a directive's capabilities.\nFinding and testing all components that use the directive is tedious, brittle, and almost as unlikely to afford full coverage.",
"translation": "但是,测试单一的用例一般无法探索该指令的全部能力。\n查找和测试所有使用该指令的组件非常繁琐和脆弱,并且通常无法覆盖所有组件。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "_Class-only tests_ might be helpful,\nbut attribute directives like this one tend to manipulate the DOM.\nIsolated unit tests don't touch the DOM and, therefore,\ndo not inspire confidence in the directive's efficacy.",
+ "translation": "*只针对类的测试*可能很有用,\n但是像这个一样的属性型指令肯定要操纵 DOM。\n隔离出的单元测试不能接触 DOM,因此也就没办法证明该指令的有效性。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "A better solution is to create an artificial test component that demonstrates all ways to apply the directive.",
- "translation": "更好的方法是创建一个展示所有使用该组件的方法的人工测试组件。",
+ "translation": "更好的方法是创建一个能展示该指令所有用法的人造测试组件。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25554,6 +29694,11 @@
"translation": "`DebugElement.properties`让我们可以访问由指令设置的自定义属性。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "## Pipe Testing",
+ "translation": "## 管道测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Pipes are easy to test without the Angular testing utilities.",
"translation": "管道很容易测试,无需Angular测试工具。",
@@ -25574,6 +29719,11 @@
"translation": "任何使用正则表达式的类都值得彻底的进行测试。\n使用Jasmine来探索预期的用例和极端的用例。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### Write DOM tests too",
+ "translation": "#### 也能编写 DOM 测试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "These are tests of the pipe _in isolation_.\nThey can't tell if the `TitleCasePipe` is working properly as applied in the application components.",
"translation": "有些管道的测试程序是**孤立的**。\n它们不能验证`TitleCasePipe`是否在应用到组件上时是否工作正常。",
@@ -25584,6 +29734,11 @@
"translation": "考虑像这样添加组件测试程序:",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "## Test debugging",
+ "translation": "## 测试程序的调试",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Debug specs in the browser in the same way that you debug an application.",
"translation": "在浏览器中,像调试应用一样调试测试程序spec。",
@@ -25624,11 +29779,21 @@
"translation": "刷新浏览器...然后它就会停在断点上。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "## Testing Utility APIs",
+ "translation": "## 测试工具 API",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "This section takes inventory of the most useful Angular testing features and summarizes what they do.",
"translation": "本节将最有用的Angular测试功能提取出来,并总结了它们的作用。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The Angular testing utilities include the `TestBed`, the `ComponentFixture`, and a handful of functions that control the test environment.\nThe [_TestBed_](#testbed-api-summary) and [_ComponentFixture_](#component-fixture-api-summary) classes are covered separately.",
+ "translation": "Angular 的测试工具集包括 `TestBed`、`ComponentFixture` 和一些用来控制测试环境的便捷函数。\n[`TestBed`](#testbed-api-summary) 和 [`ComponentFixture`](#component-fixture-api-summary) 部分单独讲过它们。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Here's a summary of the stand-alone functions, in order of likely utility:",
"translation": "下面是一些独立函数的总结,以使用频率排序:",
@@ -25644,11 +29809,36 @@
"translation": "说明",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Runs the body of a test (`it`) or setup (`beforeEach`) function within a special _async test zone_.\n See [discussion above](#async).",
+ "translation": "在一个特殊的* async 测试区域*中运行测试(`it`)的函数体或准备函数(`beforeEach`)。\n 参见[前面的讨论](#async)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Runs the body of a test (`it`) within a special _fakeAsync test zone_, enabling\n a linear control flow coding style. See [discussion above](#fake-async).",
+ "translation": "在一个特殊的* fakeAsync 测试区域*中运行测试(`it`)的函数体,以便启用线性风格的控制流。\n 参见[前面的讨论](#fake-async)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Simulates the passage of time and the completion of pending asynchronous activities\n by flushing both _timer_ and _micro-task_ queues within the _fakeAsync test zone_.",
+ "translation": "通过在* fakeAsync 测试区域*中刷新定时器和微任务(micro-task)队列来仿真时间的流逝以及异步活动的完成。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The curious, dedicated reader might enjoy this lengthy blog post,\n [\"_Tasks, microtasks, queues and schedules_\"](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/).",
"translation": "好奇和执着的读者可能会喜欢这篇长博客:\n \" _Tasks, microtasks, queues and schedules_ \".",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Accepts an optional argument that moves the virtual clock forward\n by the specified number of milliseconds,\n clearing asynchronous activities scheduled within that timeframe.\n See [discussion above](#tick).",
+ "translation": "接受一个可选参数,它可以把虚拟时钟往前推进特定的微秒数。\n 清除调度到那个时间帧中的异步活动。\n 参见[前面的讨论](#tick)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Injects one or more services from the current `TestBed` injector into a test function.\n It cannot inject a service provided by the component itself.\n See discussion of the [debugElement.injector](#get-injected-services).",
+ "translation": "从当前的 `TestBed` 注入器中把一个或多个服务注入到一个测试函数中。\n 它不能用于注入组件自身提供的服务。\n 参见 [`debugElement.injector`](#get-injected-services) 部分的讨论。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "When a `fakeAsync` test ends with pending timer event _tasks_ (queued `setTimeOut` and `setInterval` callbacks),\n the test fails with a clear error message.",
"translation": "当`fakeAsync`测试程序以正在运行的计时器事件**任务**(排队中的`setTimeOut`和`setInterval`的回调)结束时,\n 测试会失败,并显示一条明确的错误信息。",
@@ -25669,11 +29859,21 @@
"translation": "一般来说,测试应该等待微任务结束。\n 当待执行微任务存在时,调用`flushMicrotasks`来触发**微任务**队列,防止该错误发生。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "A provider token for a service that turns on [automatic change detection](#automatic-change-detection).",
+ "translation": "一个服务提供商令牌,用于开启[自动变更检测](#automatic-change-detection)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Gets the current instance of the `TestBed`.\n Usually unnecessary because the static class methods of the `TestBed` class are typically sufficient.\n The `TestBed` instance exposes a few rarely used members that are not available as\n static methods.",
"translation": "获取当前`TestBed`实例。\n 通常用不上,因为`TestBed`的静态类方法已经够用。\n `TestBed`实例有一些很少需要用到的方法,它们没有对应的静态方法。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### _TestBed_ class summary",
+ "translation": "#### `TestBed` 类小结",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The `TestBed` class is one of the principal Angular testing utilities.\nIts API is quite large and can be overwhelming until you've explored it,\na little at a time. Read the early part of this guide first\nto get the basics before trying to absorb the full API.",
"translation": "`TestBed`类是Angular测试工具的主要类之一。它的API很庞大,可能有点过于复杂,直到你一点一点的探索它们。\n阅读本章前面的部分,了解了基本的知识以后,再试着了解完整API。",
@@ -25729,6 +29929,11 @@
"translation": "调用`configureTestingModule`来为一套特定的测试定义测试模块配置,添加和删除导入、(组件、指令和管道的)声明和服务提供商。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Compile the testing module asynchronously after you've finished configuring it.\n You **must** call this method if _any_ of the testing module components have a `templateUrl`\n or `styleUrls` because fetching component template and style files is necessarily asynchronous.\n See [above](#compile-components).",
+ "translation": "在配置好测试模块之后,异步编译它。\n 如果测试模块中的*任何一个*组件具有 `templateUrl` 或 `styleUrls`,那么你**必须**调用这个方法,因为获取组件的模板或样式文件必须是异步的。\n 参见[前面的讨论](#compile-components)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "After calling `compileComponents`, the `TestBed` configuration is frozen for the duration of the current spec.",
"translation": "调用完`compileComponents`之后,`TestBed`的配置就会在当前测试期间被冻结。",
@@ -25764,6 +29969,21 @@
"translation": "从当前`TestBed`注入器获取一个服务。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The `inject` function is often adequate for this purpose.\n But `inject` throws an error if it can't provide the service.",
+ "translation": "`inject` 函数通常都能胜任这项工作,但是如果它没法提供该服务时就会抛出一个异常。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "What if the service is optional?",
+ "translation": "如果该服务是可选的呢?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The `TestBed.get()` method takes an optional second parameter,\n the object to return if Angular can't find the provider\n (`null` in this example):",
+ "translation": "`TestBed.get()` 方法可以接受可选的第二参数,当 Angular 找不到指定的服务提供商时,就会返回该对象(下面这个例子中是 `null` ):",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "After calling `get`, the `TestBed` configuration is frozen for the duration of the current spec.",
"translation": "一旦调用,`TestBed`的配置就会在当前测试期间被冻结。",
@@ -25781,7 +30001,7 @@
},
{
"original": "You may call this method _exactly once_. If you must change\n this default in the middle of your test run, call `resetTestEnvironment` first.",
- "translation": "这个方法只能被调用**一次**。如果确实需要在测试程序运行期间变换这个默认设置,那么先调用`resetTestEnvironment`。",
+ "translation": "这个方法只能被调用**一次**。如果确实需要在测试程序运行期间改变这个默认设置,那么先调用`resetTestEnvironment`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25799,14 +30019,24 @@
"translation": "少数`TestBed`实例方法没有对应的静态方法。它们很少被使用。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### The _ComponentFixture_",
+ "translation": "#### `ComponentFixture` 类",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The `TestBed.createComponent`\ncreates an instance of the component `T`\nand returns a strongly typed `ComponentFixture` for that component.",
- "translation": "`TestBed.createComponent`创建一个组件`T`的实例,并为该组件返回一个强类型的`ComponentFixture`。",
+ "translation": "`TestBed.createComponent` 会创建一个组件`T`的实例,并为该组件返回一个强类型的 `ComponentFixture`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
"original": "The `ComponentFixture` properties and methods provide access to the component,\nits DOM representation, and aspects of its Angular environment.",
- "translation": "`ComponentFixture`的属性和方法提供了对组件、它的DOM和它的Angular环境方面的访问。",
+ "translation": "`ComponentFixture` 的属性和方法提供了对组件、它的 DOM 和它的 Angular 环境方面的访问。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### _ComponentFixture_ properties",
+ "translation": "#### `ComponentFixture` 的属性",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
{
@@ -25834,6 +30064,11 @@
"translation": "与组件根元素关联的`DebugElement`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The `debugElement` provides insight into the component and its DOM element during test and debugging.\n It's a critical property for testers. The most interesting members are covered [below](#debug-element-details).",
+ "translation": "`debugElement` 提供了在测试和调试期间深入探查组件及其 DOM 元素的功能。\n 它对于测试者是一个极其重要的属性。它的大多数主要成员在[后面](#debug-element-details)都有讲解。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The native DOM element at the root of the component.",
"translation": "组件的原生根DOM元素。",
@@ -25849,6 +30084,11 @@
"translation": "在测试一个拥有`ChangeDetectionStrategy.OnPush`的组件,或者在组件的变化测试在你的程序控制下时,`ChangeDetectorRef`是最重要的。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### _ComponentFixture_ methods",
+ "translation": "#### `ComponentFixture` 的方法",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The _fixture_ methods cause Angular to perform certain tasks on the component tree.\nCall these method to trigger Angular behavior in response to simulated user action.",
"translation": "**fixture**方法使Angular对组件树执行某些任务。\n在触发Angular行为来模拟的用户行为时,调用这些方法。",
@@ -25914,6 +30154,11 @@
"translation": "返回一个承诺,在fixture稳定时解析。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "To resume testing after completion of asynchronous activity or\n asynchronous change detection, hook that promise.\n See [above](#when-stable).",
+ "translation": "要想在完成了异步活动或异步变更检测之后再继续测试,可以对那个承诺对象进行挂钩。\n 参见 [前面](#when-stable)。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Trigger component destruction.",
"translation": "触发组件的销毁。",
@@ -25949,6 +30194,16 @@
"translation": "与浏览器中DOM元素对应(WebWorkers时,值为null)。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Calling `query(predicate: Predicate)` returns the first `DebugElement`\n that matches the [predicate](#query-predicate) at any depth in the subtree.",
+ "translation": "调用 `query(predicate: Predicate)` 会在子树的任意深度中查找能和[谓词函数](#query-predicate)匹配的第一个 `DebugElement`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Calling `queryAll(predicate: Predicate)` returns all `DebugElements`\n that matches the [predicate](#query-predicate) at any depth in subtree.",
+ "translation": "调用 `queryAll(predicate: Predicate)` 会在子树的任意深度中查找能和[谓词函数](#query-predicate)匹配的所有 `DebugElement`。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "The host dependency injector.\n For example, the root element's component instance injector.",
"translation": "宿主依赖注入器。\n 比如,根元素的组件实例注入器。",
@@ -25969,6 +30224,11 @@
"translation": "当一个元素被`*ngFor`重复,它的上下文为`NgForRow`,它的`$implicit`属性值是该行的实例值。\n 比如,`*ngFor=\"let hero of heroes\"`里的`hero`。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "The immediate `DebugElement` children. Walk the tree by descending through `children`.",
+ "translation": "`DebugElement` 的直接子元素。可以通过继续深入 `children` 来遍历这棵树。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "`DebugElement` also has `childNodes`, a list of `DebugNode` objects.\n `DebugElement` derives from `DebugNode` objects and there are often\n more nodes than elements. Testers can usually ignore plain nodes.",
"translation": "`DebugElement`还有`childNodes`,即`DebugNode`对象列表。\n `DebugElement`从`DebugNode`对象衍生,而且通常节点(node)比元素多。测试者通常忽略赤裸节点。",
@@ -25984,6 +30244,11 @@
"translation": "元素的标签名字,如果它是一个元素的话。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "Triggers the event by its name if there is a corresponding listener\n in the element's `listeners` collection.\n The second parameter is the _event object_ expected by the handler.\n See [above](#trigger-event-handler).",
+ "translation": "如果在该元素的 `listeners` 集合中有相应的监听器,就根据名字触发这个事件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "If the event lacks a listener or there's some other problem,\n consider calling `nativeElement.dispatchEvent(eventObject)`.",
"translation": "如果事件缺乏监听器,或者有其它问题,考虑调用`nativeElement.dispatchEvent(eventObject)`。",
@@ -26039,6 +30304,16 @@
"translation": "`By.directive(directive)` - 返回Angular能匹配一个指令类实例的所有元素。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "## Frequently Asked Questions",
+ "translation": "## 常见问题",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "#### Why put spec file next to the file it tests?",
+ "translation": "#### 为什么要把测试文件和被测文件放在一起?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "It's a good idea to put unit test spec files in the same folder\nas the application source code files that they test:",
"translation": "我们推荐将单元测试的spec配置文件放到与应用程序源代码文件所在的同一个文件夹中,因为:",
@@ -26069,6 +30344,11 @@
"translation": "当你重命名源代码文件(无可避免),你记得重命名测试程序文件。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### When would I put specs in a test folder?",
+ "translation": "#### 什么时候我该把测试文件放进单独的 `test` 文件夹中?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "Application integration specs can test the interactions of multiple parts\nspread across folders and modules.\nThey don't really belong to any part in particular, so they don't have a\nnatural home next to any one file.",
"translation": "应用程序的整合测试spec文件可以测试横跨多个目录和模块的多个部分之间的互动。\n它们不属于任何部分,很自然,没有特别的地方存放它们。",
@@ -26084,6 +30364,46 @@
"translation": "当然,**测试助手对象**的测试spec文件也属于`test`目录,与它们对应的助手文件相邻。",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
},
+ {
+ "original": "#### Why not rely on E2E tests of DOM integration?",
+ "translation": "#### 为什么不依赖 E2E 测试来保障 DOM 集成后的正确性?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "The component DOM tests describe in this guide often require extensive setup and \nadvanced techniques where as the [class-only test](#component-class-testing)\nwere comparatively simple.",
+ "translation": "本指南中讲的组件 DOM 测试通常需要大量的准备工作以及高级技巧,不像[只针对类的测试](#component-class-testing)那样简单。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Why not defer DOM integration tests to end-to-end (E2E) testing?",
+ "translation": "为什么不等到端到端(E2E)测试阶段再对 DOM 进行集成测试呢?",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "E2E tests are great for high-level validation of the entire system.\nBut they can't give you the comprehensive test coverage that you'd expect from unit tests.",
+ "translation": "E2E 测试对于整个系统的高层验证非常好用。\n但是它们没法给你像单元测试这样全面的测试覆盖率。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "E2E tests are difficult to write and perform poorly compared to unit tests.\nThey break easily, often due to changes or misbehavior far removed from the site of breakage.",
+ "translation": "E2E 测试很难写,并且执行性能也赶不上单元测试。\n它们很容易被破坏,而且经常是因为某些远离故障点的修改或不当行为而导致的。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "E2E tests can't easily reveal how your components behave when things go wrong,\nsuch as missing or bad data, lost connectivity, and remote service failures.",
+ "translation": "当出错时,E2E 测试不能轻松揭露你的组件出了什么问题,\n比如丢失或错误的数据、网络失去连接或远端服务器挂了。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "E2E tests for apps that update a database, \nsend an invoice, or charge a credit card require special tricks and back-doors to prevent\naccidental corruption of remote resources.\nIt can even be hard to navigate to the component you want to test.",
+ "translation": "如果 E2E 的测试对象要更新数据库、发送发票或收取信用卡,就需要一些特殊的技巧和后门来防止远程资源被意外破坏。\n它甚至可能都难以导航到你要测试的组件。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
+ {
+ "original": "Because of these many obstacles, you should test DOM interaction\nwith unit testing techniques as much as possible.",
+ "translation": "由于存在这么多障碍,你应该尽可能使用单元测试技术来测试 DOM 交互。",
+ "sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/testing.md"
+ },
{
"original": "# TypeScript Configuration",
"translation": "# TypeScript 配置",
@@ -32236,7 +36556,7 @@
},
{
"original": "Install the *In-memory Web API* package from _npm_",
- "translation": "从 `npm` 中安装这个*内存 Web API* 包",
+ "translation": "从 `npm` 中安装这个*内存 Web API* 包(译注:请使用 0.5+ 的版本,不要使用 0.4-)",
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/tutorial/toh-pt6.md"
},
{
diff --git a/aio/tools/translator/translate.spec.ts b/aio/tools/translator/translate.spec.ts
index 7019030652..1b3974d499 100644
--- a/aio/tools/translator/translate.spec.ts
+++ b/aio/tools/translator/translate.spec.ts
@@ -1,5 +1,6 @@
import { expect } from 'chai';
-import { kernelText, lookup } from './translate';
+import { lookup } from './translate';
+import { kernelText } from './utils';
describe('根据字典进行翻译', () => {
diff --git a/aio/tools/translator/translate.ts b/aio/tools/translator/translate.ts
index 1497027017..563c27b843 100644
--- a/aio/tools/translator/translate.ts
+++ b/aio/tools/translator/translate.ts
@@ -3,24 +3,19 @@ import * as _ from 'lodash';
import { DictEntry } from './dict-entry';
import { dirs } from './dirs';
import { listMarkdownFiles } from './extractor';
-import { indentOf, normalizeLines, repeat } from './utils';
+import { exactlyTest, indentOf, normalizeLines, repeat } from './utils';
+
+// TODO: 改用 markdown 解析器实现
export const dict = require('./dict-latest.json') as DictEntry[];
export function lookup(english: string, filename: RegExp = /.*/): DictEntry[] {
const entries = dict
.filter(entry => filename.test(entry.sourceFile))
- .filter(entry => kernelText(entry.original) === kernelText(english));
+ .filter(entry => exactlyTest(entry.original, english));
return _.uniqBy(entries, 'translation');
}
-export function kernelText(text: string): string {
- return text
- .replace(/[\s\n]+/g, '')
- .replace(/\.$/g, '')
- .trim();
-}
-
export function translate(content: string): string[] {
const lines = normalizeLines(content)
.split(/\n+\s*\n+/);
diff --git a/aio/tools/translator/utils.spec.ts b/aio/tools/translator/utils.spec.ts
index 8fda4f0e56..232c428578 100644
--- a/aio/tools/translator/utils.spec.ts
+++ b/aio/tools/translator/utils.spec.ts
@@ -1,5 +1,5 @@
import { expect } from 'chai';
-import { normalizeLines } from './utils';
+import { fuzzyTest, normalizeLines, tokenize } from './utils';
describe(' 工具函数', () => {
it('把“1. ”列表处理成空行分隔的格式,以便处理', function () {
@@ -338,4 +338,16 @@ a c
`);
});
+ it('拆分', function () {
+ expect(tokenize('abc def,abc.')).eql(['abc', 'def', 'abc']);
+ });
+
+ it('模糊匹配', function () {
+ expect(fuzzyTest(`a b c d e`, `a b c d e`)).is.false;
+ expect(fuzzyTest(`a b c d e f g`, `a b c d e`)).is.false;
+ expect(fuzzyTest(`Make that easy by encapsulating the _click-triggering_ process in a helper such as the \`click\` function below:`,
+ `Make that consistent and easy by encapsulating the _click-triggering_ process
+in a helper such as the \`click()\` function below:
+`)).is.true;
+ });
});
diff --git a/aio/tools/translator/utils.ts b/aio/tools/translator/utils.ts
index 35e0ff7d01..05afae62b0 100644
--- a/aio/tools/translator/utils.ts
+++ b/aio/tools/translator/utils.ts
@@ -1,5 +1,6 @@
import { DictEntry } from './dict-entry';
import { isTranslation } from './extractor';
+import * as _ from 'lodash';
export function translationHasNotCodeExample(entry: DictEntry): boolean {
return entry.translation.indexOf(' 5 && sameTokens.length / maxTokens >= 0.8;
+}
+
+export function exactlyTest(text1: string, text2: string): boolean {
+ return kernelText(text1) === kernelText(text2);
+}
+
+export function kernelText(text: string): string {
+ return text
+ .replace(/[\s\n]+/g, '')
+ .replace(/\.$/g, '')
+ .toUpperCase()
+ .trim();
+}
+
+export function tokenize(text: string): string[] {
+ return text.split(/\W/)
+ .map(token => token.trim())
+ .filter(token => !!token);
+}