2016-02-05 23:27:06 -08:00
|
|
|
|
include ../_util-fns
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
|
|
|
|
# Services
|
2016-04-16 16:43:13 +08:00
|
|
|
|
# 服务
|
2015-12-16 21:47:02 -05:00
|
|
|
|
The Tour of Heroes is evolving and we anticipate adding more components in the near future.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
《英雄指南》继续前行。接下来,我们准备添加更多的组件。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Multiple components will need access to hero data and we don't want to copy and
|
|
|
|
|
paste the same code over and over.
|
|
|
|
|
Instead, we'll create a single reusable data service and learn to
|
|
|
|
|
inject it in the components that need it.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
将来会有更多的组件访问英雄数据,但我们不想一遍又一遍的复制粘贴同样的代码。
|
|
|
|
|
我们的替代方案是,创建一个单一的、可复用的数据服务,然后学着把它注入到那些想用它的组件中去。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Refactoring data access to a separate service keeps the component lean and focused on supporting the view.
|
|
|
|
|
It also makes it easier to unit test the component with a mock service.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们将重构数据访问代码,把它隔离到一个独立的服务中去,让组件尽可能保持精简,专注于为视图提供支持。
|
|
|
|
|
在这种方式下,借助mock服务来对组件进行单元测试也会更容易。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Because data services are invariably asynchronous,
|
|
|
|
|
we'll finish the chapter with a promise-based version of the data service.
|
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
因为数据服务通常都是异步的,所以在本章的最后,我们会把它重构为基于承诺(Promise,一种异步编程模式)的版本。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-06-07 15:51:25 -07:00
|
|
|
|
p Run the #[+liveExampleLink2('', 'toh-4')] for this part.
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2016-06-08 09:58:16 +08:00
|
|
|
|
p 运行这部分的#[+liveExampleLink2('在线例子', 'toh-4')]。
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
2015-12-16 21:47:02 -05:00
|
|
|
|
## Where We Left Off
|
2016-04-16 16:43:13 +08:00
|
|
|
|
## 我们在哪儿
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Before we continue with our Tour of Heroes, let’s verify we have the following structure.
|
|
|
|
|
If not, we’ll need to go back and follow the previous chapters.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
在继续《英雄指南》之前,先来检查一下,你是否已经有了如下目录结构。如果没有,你得先回上一章,看看错过了哪里。
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.filetree
|
|
|
|
|
.file angular2-tour-of-heroes
|
|
|
|
|
.children
|
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.ts
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.file hero.ts
|
|
|
|
|
.file hero-detail.component.ts
|
2016-02-11 15:08:06 -08:00
|
|
|
|
.file main.ts
|
|
|
|
|
.file node_modules ...
|
2016-03-15 13:24:50 +01:00
|
|
|
|
.file typings ...
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.file index.html
|
|
|
|
|
.file package.json
|
2016-04-27 11:28:22 -07:00
|
|
|
|
.file styles.css
|
|
|
|
|
.file systemjs.config.js
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.file tsconfig.json
|
2016-02-11 15:08:06 -08:00
|
|
|
|
.file typings.json
|
2016-06-07 15:51:25 -07:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
|
|
|
|
### Keep the app transpiling and running
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### 让应用代码保持转译和运行
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Open a terminal/console window.
|
|
|
|
|
Start the TypeScript compiler, watch for changes, and start our server by entering the command:
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
打开terminal/console窗口,启动TypeScript编译器,它会监视文件变更,并启动开发服务器。只要敲:
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2016-06-07 15:51:25 -07:00
|
|
|
|
code-example(language="bash").
|
2015-12-16 21:47:02 -05:00
|
|
|
|
npm start
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2015-12-16 21:47:02 -05:00
|
|
|
|
The application runs and updates automatically as we continue to build the Tour of Heroes.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
当我们继续构建《英雄指南》时,应用会自动运行和更新。
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
## Creating a Hero Service
|
2016-04-16 16:43:13 +08:00
|
|
|
|
## 创建英雄服务
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Our stakeholders have shared their larger vision for our app.
|
|
|
|
|
They tell us they want to show the heroes in various ways on different pages.
|
|
|
|
|
We already can select a hero from a list.
|
|
|
|
|
Soon we'll add a dashboard with the top performing heroes and create a separate view for editing hero details.
|
|
|
|
|
All three views need hero data.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
客户向我们描绘了本应用更大的目标。
|
|
|
|
|
他们说,想要在不同的页面中用多种方式显示英雄。
|
|
|
|
|
现在我们已经能从列表中选择一个英雄了,但这还不够。
|
|
|
|
|
很快,我们将添加一个仪表盘来表彰绩效最好的英雄,并且创建一个独立视图来编辑英雄的详情。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
所有这些视图都需要英雄的数据。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
At the moment the `AppComponent` defines mock heroes for display.
|
|
|
|
|
We have at least two objections.
|
|
|
|
|
First, defining heroes is not the component's job.
|
|
|
|
|
Second, we can't easily share that list of heroes with other components and views.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
目前,`AppComponent`显示的是我们自定义的一个mock英雄数据。
|
|
|
|
|
我们可改进的地方至少有两个:首先,定义英雄的数据不该是该组件的任务。其次,想把这份英雄列表的数据共享给其它组件和视图可不那么容易。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We can refactor this hero data acquisition business to a single service that provides heroes and
|
|
|
|
|
share that service with all components that need heroes.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们可以把提供英雄数据的任务重构为一个单独的服务,它将提供英雄数据,并且把这个服务在所有需要英雄数据的组件之间共享。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
### Create the HeroService
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### 创建HeroService
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Create a file in the `app` folder called `hero.service.ts`.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
|
|
|
|
在`app`目录下创建一个名叫`hero.service.ts`的文件。
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.l-sub-section
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
:marked
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We've adopted a convention in which we spell the name of a service in lowercase followed by `.service`.
|
2016-03-15 13:24:50 +01:00
|
|
|
|
If the service name were multi-word, we'd spell the base filename with lower dash case (AKA "kebab-case").
|
2015-12-16 21:47:02 -05:00
|
|
|
|
The `SpecialSuperHeroService` would be defined in the `special-super-hero.service.ts` file.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
我们遵循的文件命名约定是:服务名称的小写形式(基本名),加上`.service`后缀。
|
|
|
|
|
如果服务名称包含多个单词,我们就把基本名部分写成中线形式(dash-case,也被称作烤串形式kebab-case)。
|
2016-05-12 17:50:14 +08:00
|
|
|
|
比如,`SpecialSuperHeroService`服务应该被定义在`special-super-hero.service.ts`文件中。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
We name the class `HeroService` and export it for others to import.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
|
|
|
|
我们把这个类命名为`HeroService`,并且导出它,以供别人使用。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2016-04-16 16:43:13 +08:00
|
|
|
|
+makeExample('toh-4/ts/app/hero.service.1.ts', 'empty-class', 'hero.service.ts (导出类)')(format=".")
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
|
|
|
|
:marked
|
2015-12-16 21:47:02 -05:00
|
|
|
|
### Injectable Services
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### 可注入的服务
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Notice that we imported the Angular `Injectable` function and applied that function as an `@Injectable()` decorator.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
注意,我们引入了Angular的`Injectable`函数,并通过`@Injectable()`装饰器使用这个函数。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.callout.is-helpful
|
|
|
|
|
:marked
|
|
|
|
|
**Don't forget the parentheses!** Neglecting them leads to an error that's difficult to diagnose.
|
2016-05-12 17:50:14 +08:00
|
|
|
|
|
|
|
|
|
**不要忘了写圆括号!**如果忘了写,就会导致一个很难诊断的错误。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
TypeScript sees the `@Injectable()` decorator and emits metadata about our service,
|
|
|
|
|
metadata that Angular may need to inject other dependencies into this service.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
当TypeScript看到`@Injectable()`装饰器时,就会记下本服务的元数据。如果Angular需要往这个服务中注入其它依赖,就会使用这些元数据。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
The `HeroService` doesn't have any dependencies *at the moment*. Add the decorator anyway.
|
2016-06-13 17:20:08 +08:00
|
|
|
|
It is a "best practice" to apply the `@Injectable()` decorator *from the start*
|
2015-12-16 21:47:02 -05:00
|
|
|
|
both for consistency and for future-proofing.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
此刻,`HeroService`还没有任何依赖,但我们还是得加上这个装饰器。作为一项最佳实践,无论是出于提高统一性还是减少变更的目的,都应该从一开始就加上`@Injectable()`装饰器。
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
### Getting Heroes
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### 获取英雄
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Add a `getHeroes` method stub.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
添加一个名叫`getHeros`的桩方法。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
+makeExample('toh-4/ts/app/hero.service.1.ts', 'getHeroes-stub', 'hero.service.ts (getHeroes桩方法)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
We're holding back on the implementation for a moment to make an important point.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
在这个实现上暂停一下,我们先来讲一个重点。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
The consumer of our service doesn't know how the service gets the data.
|
|
|
|
|
Our `HeroService` could get `Hero` data from anywhere.
|
|
|
|
|
It could get the data from a web service or local storage
|
|
|
|
|
or from a mock data source.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
消费者并不知道本服务会如何获取数据。
|
|
|
|
|
我们的`HeroService`服务可以从任何地方获取英雄的数据。
|
|
|
|
|
它可以从网络服务器获取,可以从浏览器的局部存储区获取,也可以是直接写在源码中的mock数据。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
That's the beauty of removing data access from the component.
|
|
|
|
|
We can change our minds about the implementation as often as we like,
|
|
|
|
|
for whatever reason, without touching any of the components that need heroes.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们从组件中成功移除了数据访问代码,干得漂亮。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
这下子,我们可以随时改变数据访问的实现方式了。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
### Mock Heroes
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### Mock英雄数据
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We already have mock `Hero` data sitting in the `AppComponent`. It doesn't belong there. It doesn't belong *here* either.
|
|
|
|
|
We'll move the mock data to its own file.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们曾在`AppComponent`组件中写过mock版的英雄数据。它不该在那里,但也不该在*这里*!我们得把mock数据移到它自己的文件中去。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Cut the `HEROES` array from `app.component.ts` and paste it to a new file in the `app` folder named `mock-heroes.ts`.
|
2016-03-24 13:31:27 +01:00
|
|
|
|
We copy the `import {Hero} ...` statement as well because the heroes array uses the `Hero` class.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
从`app.component.ts`文件中剪切`HEROS`数组,并把它粘贴到`app`目录下一个名叫`mock-heroes.ts`的文件中。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
我们还要把`import {Hero}...`语句拷贝过来,因为我们的英雄数组用到了`Hero`类。
|
|
|
|
|
+makeExample('toh-4/ts/app/mock-heroes.ts', null, 'mock-heroes.ts (英雄数组)')
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
We export the `HEROES` constant so we can import it elsewhere — such as our `HeroService`.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们导出了`HEROES`常量,以便在其它地方导入它 —— 比如`HeroService`服务。
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Meanwhile, back in `app.component.ts` where we cut away the `HEROES` array,
|
|
|
|
|
we leave behind an uninitialized `heroes` property:
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
同时,回到刚剪切出`HEROES`数组的`app.component.ts`文件,我们留下了一个尚未初始化的`heroes`属性:
|
2016-04-16 16:43:13 +08:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'heroes-prop', 'app.component.ts (heroes属性)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
### Return Mocked Heroes
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### 返回模拟的英雄数据
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Back in the `HeroService` we import the mock `HEROES` and return it from the `getHeroes` method.
|
|
|
|
|
Our `HeroService` looks like this:
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
回到`HeroService`,我们导入`HEROES`常量,并在`getHeroes`方法中返回它。
|
|
|
|
|
我们的`HeroService`服务现在看起来是这样:
|
2015-12-16 21:47:02 -05:00
|
|
|
|
+makeExample('toh-4/ts/app/hero.service.1.ts', null, 'hero.service.ts')(format=".")
|
|
|
|
|
:marked
|
|
|
|
|
### Use the Hero Service
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### 使用HeroService服务
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We're ready to use the `HeroService` in other components starting with our `AppComponent`.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2016-04-16 16:43:13 +08:00
|
|
|
|
我们已经在包括`AppComponent`在内的多个组件中使用了`HeroService`服务。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We begin, as usual, by importing the thing we want to use, the `HeroService`.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
通常,我们会从导入要用的东西开始,比如`HeroService`。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.ts', 'hero-service-import', 'app.component.ts (导入HeroService)')
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
Importing the service allows us to *reference* it in our code.
|
|
|
|
|
How should the `AppComponent` acquire a runtime concrete `HeroService` instance?
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
导入这个服务让我们可以在代码中引用它。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
`AppComponent`该如何在运行中获得一个具体的`HeroService`实例呢?
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
### Do we *new* the *HeroService*? No way!
|
2016-06-14 07:43:48 +08:00
|
|
|
|
### 我们要自己 *new* 出这个 *HeroService* 吗?不!
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We could create a new instance of the `HeroService` with "new" like this:
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
固然,我们可以使用`new`关键字来创建`HeroService`的实例,就像这样:
|
2015-12-16 21:47:02 -05:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'new-service')(format=".")
|
|
|
|
|
:marked
|
|
|
|
|
That's a bad idea for several reasons including
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
但这不是个好主意,有很多理由,比如:
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
* Our component has to know how to create a `HeroService`.
|
|
|
|
|
If we ever change the `HeroService` constructor,
|
|
|
|
|
we'll have to find every place we create the service and fix it.
|
|
|
|
|
Running around patching code is error prone and adds to the test burden.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-06-14 07:43:48 +08:00
|
|
|
|
* 我们的组件将不得不弄清楚该如何创建`HeroService`。
|
2016-05-12 17:50:14 +08:00
|
|
|
|
如果有一天我们修改了`HeroService`的构造函数,我们不得不找出创建过此服务的每一处代码,并修改它。
|
|
|
|
|
而给代码打补丁的行为容易导致错误,并增加了测试的负担。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
* We create a new service each time we use "new".
|
|
|
|
|
What if the service should cache heroes and share that cache with others?
|
|
|
|
|
We couldn't do that.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
* 我们每次使用`new`都会创建一个新的服务实例。
|
|
|
|
|
如果这个服务需要缓存英雄列表,并把这个缓存共享给别人呢?怎么办?
|
|
|
|
|
没办法,做不到。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
* We're locking the `AppComponent` into a specific implementation of the `HeroService`.
|
|
|
|
|
It will be hard to switch implementations for different scenarios.
|
|
|
|
|
Can we operate offline?
|
|
|
|
|
Will we need different mocked versions under test?
|
|
|
|
|
Not easy.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
* 我们把`AppComponent`锁死在`HeroService`的一个特定实现中。
|
|
|
|
|
我们很难在别的场景中把它换成别的实现。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
比如,能离线操作吗?能在测试时使用不同的模拟版本吗?这可不容易。
|
|
|
|
|
|
2016-06-13 17:20:08 +08:00
|
|
|
|
* What if ... what if ... Hey, we've got work to do!*
|
2016-05-12 17:50:14 +08:00
|
|
|
|
* 如果……如果……嘿!这下我们可有得忙了!
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
We get it. Really we do.
|
|
|
|
|
But it is so ridiculously easy to avoid these problems that there is no excuse for doing it wrong.
|
|
|
|
|
|
2016-06-14 07:43:48 +08:00
|
|
|
|
有办法了,真的!这个办法真是简单得不可思议,它能解决这些问题,你就再也没有犯错误的借口了。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
### Inject the *HeroService*
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### 注入 *HeroService*
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2016-05-10 19:49:54 +02:00
|
|
|
|
Two lines replace the one line that created with *new*:
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
用这两行代码代替用`new`时的一行:
|
2016-04-20 20:07:39 +08:00
|
|
|
|
1. we add a constructor.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
1. 添加一个构造函数
|
2016-04-20 20:07:39 +08:00
|
|
|
|
1. we add to the component's `providers` metadata
|
2016-04-16 16:43:13 +08:00
|
|
|
|
1. 添加组件的`providers`元数据
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Here's the constructor:
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
|
|
|
|
下面就是这个构造函数:
|
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'ctor', 'app.component.ts (构造函数)')
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
The constructor itself does nothing. The parameter simultaneously
|
2016-05-03 14:06:32 +02:00
|
|
|
|
defines a private `heroService` property and identifies it as a `HeroService` injection site.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-19 13:33:02 +08:00
|
|
|
|
构造函数自己什么也不用做,它在参数中定义了一个私有的`heroService`属性,并把它标记为注入`HeroService`的靶点。
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Now Angular will know to supply an instance of the `HeroService` when it creates a new `AppComponent`.
|
|
|
|
|
|
2016-04-16 16:43:13 +08:00
|
|
|
|
现在,Angular将会知道,当它创建`AppComponent`实例时,需要先提供一个`HeroService`的实例。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
Learn more about Dependency Injection in the [Dependency Injection](../guide/dependency-injection.html) chapter.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
|
|
|
|
要了解关于依赖注入的更多知识,请参见[依赖注入](../guide/dependency-injection.html)一章。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
The *injector* does not know yet how to create a `HeroService`.
|
|
|
|
|
If we ran our code now, Angular would fail with an error:
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
*注入器*还不知道该如何创建`HeroService`。
|
|
|
|
|
如果现在运行我们的代码,Angular就会失败,并报错:
|
2015-12-14 20:05:13 -08:00
|
|
|
|
code-example(format="." language="html").
|
|
|
|
|
EXCEPTION: No provider for HeroService! (AppComponent -> HeroService)
|
2016-06-14 07:43:48 +08:00
|
|
|
|
(异常:没有HeroService的供应商!(AppComponent -> HeroService))
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
We have to teach the *injector* how to make a `HeroService` by registering a `HeroService` **provider**.
|
|
|
|
|
Do that by adding the following `providers` array property to the bottom of the component metadata
|
|
|
|
|
in the `@Component` call.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2016-05-14 22:23:18 +08:00
|
|
|
|
我们还得注册一个`HeroService`**供应商**,来告诉*注入器*如何创建`HeroService`。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
要做到这一点,我们应该在`@Component`组件的元数据底部添加`providers`数组属性如下:
|
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'providers', 'app.component.ts (提供HeroService)')
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
The `providers` array tells Angular to create a fresh instance of the `HeroService` when it creates a new `AppComponent`.
|
|
|
|
|
The `AppComponent` can use that service to get heroes and so can every child component of its component tree.
|
2016-06-13 17:20:08 +08:00
|
|
|
|
|
|
|
|
|
`providers`数组告诉Angular,当它创建新的`AppComponent`组件时,也要创建一个`HeroService`的新实例。
|
|
|
|
|
`AppComponent`会使用那个服务来获取影响列表,在它组件树中的每一个子组件也同样如此。
|
2016-05-10 19:49:54 +02:00
|
|
|
|
a#child-component
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2015-12-16 21:47:02 -05:00
|
|
|
|
### *getHeroes* in the *AppComponent*
|
2016-04-16 16:43:13 +08:00
|
|
|
|
### *AppComponent* 中的 *getHeroes*
|
2016-05-03 14:06:32 +02:00
|
|
|
|
We've got the service in a `heroService` private variable. Let's use it.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2016-05-19 13:33:02 +08:00
|
|
|
|
我们已经获得了此服务,并把它存入了私有变量`heroService`中。我们这就开始使用它。
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We pause to think. We can call the service and get the data in one line.
|
2016-04-16 16:43:13 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
停下来想一想。我们可以在同一行内调用此服务并获得数据。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'get-heroes')(format=".")
|
|
|
|
|
:marked
|
|
|
|
|
We don't really need a dedicated method to wrap one line. We write it anyway:
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
在真实的世界中,我们并不需要把一行代码包装成一个专门的方法,但无论如何,我们在演示代码中先这么写:
|
2015-12-16 21:47:02 -05:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'getHeroes', 'app.component.ts (getHeroes)')(format=".")
|
2016-01-23 18:21:09 +00:00
|
|
|
|
|
2016-03-16 23:49:55 +01:00
|
|
|
|
<a id="oninit"></a>
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
### The *ngOnInit* Lifecycle Hook
|
2016-04-17 10:24:39 +08:00
|
|
|
|
### *ngOnInit* 生命周期钩子
|
2015-12-16 21:47:02 -05:00
|
|
|
|
`AppComponent` should fetch and display heroes without a fuss.
|
|
|
|
|
Where do we call the `getHeroes` method? In a constructor? We do *not*!
|
|
|
|
|
|
2016-04-17 10:24:39 +08:00
|
|
|
|
毫无疑问,`AppComponent`应该获取英雄数据并显示它。
|
|
|
|
|
我们该在哪里调用`getHeroes`方法呢?在构造函数中吗? *不* !
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Years of experience and bitter tears have taught us to keep complex logic out of the constructor,
|
|
|
|
|
especially anything that might call a server as a data access method is sure to do.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
多年的经验和惨痛的教训教育我们,应该把复杂的逻辑扔到构造函数外面去,特别是那些需要从服务器获取数据的逻辑更是如此。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
The constructor is for simple initializations like wiring constructor parameters to properties.
|
|
|
|
|
It's not for heavy lifting. We should be able to create a component in a test and not worry that it
|
|
|
|
|
might do real work — like calling a server! — before we tell it to do so.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
构造函数是为了简单的初始化工作而设计的,比如把构造函数的参数赋值给属性。
|
|
|
|
|
它的负担不应该过于沉重。我们应该能在测试中创建一个组件,而不用担心它会做实际的工作 —— 比如和服务器通讯,直到我们主动要求它做这些。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
If not the constructor, something has to call `getHeroes`.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
如果不在构造函数中,总得有地方调用`getHeroes`吧。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Angular will call it if we implement the Angular **ngOnInit** *Lifecycle Hook*.
|
|
|
|
|
Angular offers a number of interfaces for tapping into critical moments in the component lifecycle:
|
|
|
|
|
at creation, after each change, and at its eventual destruction.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
这也不难。只要我们实现了Angular的 **ngOnInit** *生命周期钩子* ,Angular就会主动调用这个钩子。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
Angular提供了一些接口,用来介入组件生命周期的几个关键时间点:刚创建时、每次变化时,以及最终被销毁时。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Each interface has a single method. When the component implements that method, Angular calls it at the appropriate time.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
每个接口都有唯一的一个方法。只要组件实现了这个方法,Angular就会在合适的时机调用它。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
Learn more about lifecycle hooks in the [Lifecycle Hooks](../guide/lifecycle-hooks.html) chapter.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
要了解关于生命周期钩子的更多知识,请参见 [生命周期钩子](../guide/lifecycle-hooks.html) 一章。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
Here's the essential outline for the `OnInit` interface:
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
这是`OnInit`接口的基本轮廓:
|
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'on-init', 'app.component.ts (OnInit协议)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
We write an `ngOnInit` method with our initialization logic inside and leave it to Angular to call it
|
|
|
|
|
at the right time. In our case, we initialize by calling `getHeroes`.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们写下带有初始化逻辑的`ngOnInit`方法,然后留给Angular,供其在正确的时机调用。在这个例子中,我们通过调用`getHeroes`来完成初始化。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'ng-on-init', 'app.component.ts (OnInit协议)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
Our application should be running as expected, showing a list of heroes and a hero detail view
|
|
|
|
|
when we click on a hero name.
|
|
|
|
|
|
2016-04-17 10:24:39 +08:00
|
|
|
|
我们的应用将会像期望的那样运行,显示英雄列表,并且在我们点击英雄的名字时,显示英雄的详情。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We're getting closer. But something isn't quite right.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们就快完成了,但还有点事情不太对劲儿。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
## Async Services and Promises
|
2016-06-14 09:26:54 +08:00
|
|
|
|
## 异步服务与承诺(Promise)
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Our `HeroService` returns a list of mock heroes immediately.
|
|
|
|
|
Its `getHeroes` signature is synchronous
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
我们的`HeroService`立即返回一个模拟的英雄列表,它的`getHeroes`函数签名是同步的。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'get-heroes')(format=".")
|
|
|
|
|
:marked
|
|
|
|
|
Ask for heroes and they are there in the returned result.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
请求英雄数据,并直接在结果中返回它。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
Someday we're going to get heroes from a remote server. We don’t call http yet, but we aspire to in later chapters.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
将来,我们会从远端服务器上获取英雄数据。我们还没调用http,但在未来的章节中我们会希望这么做。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
When we do, we'll have to wait for the server to respond and we won't be able to block the UI while we wait,
|
|
|
|
|
even if we want to (which we shouldn't) because the browser won't block.
|
|
|
|
|
|
2016-04-17 10:24:39 +08:00
|
|
|
|
那时候,我们不得不等待服务器返回,并且在等待时,我们没法停止UI响应,即使我们想这么做,也做不到,因为浏览器不会停止。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We'll have to use some kind of asynchronous technique and that will change the signature of our `getHeroes` method.
|
|
|
|
|
|
2016-04-17 10:24:39 +08:00
|
|
|
|
我们不得不使用一些异步技术,而这将改变`getHeroes`方法的签名。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We'll use *promises*.
|
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
我们将使用 *承诺(Promise)* 。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
### The Hero Service makes a promise
|
2016-05-12 17:50:14 +08:00
|
|
|
|
### `HeroService`会生成一个承诺
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
A **promise** is ... well it's a promise to call us back later when the results are ready.
|
|
|
|
|
We ask an asynchronous service to do some work and give it a callback function.
|
|
|
|
|
It does that work (somewhere) and eventually it calls our function with the results of the work or an error.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
**承诺** 就是 …… 好吧,它就是一个承诺 —— 在有了结果时,它承诺会回调我们。
|
|
|
|
|
我们请求一个异步服务去做点什么,然后给它一个回调函数。
|
2016-06-14 09:26:54 +08:00
|
|
|
|
它会去做(无论用哪种方式),一旦完成,它就会调用我们的回调函数,并通过参数把工作成果或者错误信息传给我们。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2016-03-25 02:33:56 -04:00
|
|
|
|
We are simplifying. Learn about ES2015 Promises [here](http://exploringjs.com/es6/ch_promises.html) and elsewhere on the web.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
这里只是粗浅的说说,要了解更多,请参见[这里](http://exploringjs.com/es6/ch_promises.html)或在Web上搜索其它学习资源。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
Update the `HeroService` with this promise-returning `getHeroes` method:
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
把`HeroService`的`getHeroes`方法改写为返回承诺的形式:
|
2015-12-16 21:47:02 -05:00
|
|
|
|
+makeExample('toh-4/ts/app/hero.service.ts', 'get-heroes', 'hero.service.ts (getHeroes)')(format=".")
|
|
|
|
|
:marked
|
|
|
|
|
We're still mocking the data. We're simulating the behavior of an ultra-fast, zero-latency server,
|
|
|
|
|
by returning an **immediately resolved promise** with our mock heroes as the result.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们继续使用模拟数据。我们通过返回一个 *立即解决的承诺* 的方式,模拟了一个超快、零延迟的超级服务器。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
### Act on the Promise
|
2016-04-17 10:24:39 +08:00
|
|
|
|
### 基于承诺的行动
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Returning to the `AppComponent` and its `getHeroes` method, we see that it still looks like this:
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
回到`AppComponent`和它的`getHeroes`方法,我们看到它看起来还是这样的:
|
2016-04-17 10:24:39 +08:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.1.ts', 'getHeroes', 'app.component.ts (getHeroes - 老的)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
As a result of our change to `HeroService`, we're now setting `this.heroes` to a promise rather than an array of heroes.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
在修改了`HeroService`之后,我们还要把`this.heroes`替换为一个承诺,而不再是一个英雄数组。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We have to change our implementation to *act on the promise when it resolves*.
|
|
|
|
|
When the promise resolves successfully, *then* we will have heroes to display.
|
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们得修改这个实现,把它变成*基于承诺*的,并在承诺的事情被解决时再行动。
|
|
|
|
|
一旦承诺的事情被成功解决,我们就会显示英雄数据。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
We pass our callback function as an argument to the promise's **then** method:
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
我们把回调函数作为参数传给承诺对象的**then**函数:
|
2016-04-17 10:24:39 +08:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.ts', 'get-heroes', 'app.component.ts (getHeroes - 修改版)')(format=".")
|
2015-12-22 10:25:30 -08:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
The [ES2015 arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
|
2016-01-16 23:00:45 +01:00
|
|
|
|
in the callback is more succinct than the equivalent function expression and gracefully handles *this*.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
回调中所用的[ES2015箭头函数](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
|
2016-05-12 17:50:14 +08:00
|
|
|
|
能比等价的函数表达式更加快速、优雅的处理*this*指针。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
Our callback sets the component's `heroes` property to the array of heroes returned by the service. That's all there is to it!
|
|
|
|
|
|
2016-04-17 10:24:39 +08:00
|
|
|
|
在回调中,我们把由服务返回的英雄数组赋值给组件的`heroes`属性。是的,这就搞定了。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Our app should still be running, still showing a list of heroes, and still
|
|
|
|
|
responding to a name selection with a detail view.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
我们的程序仍在运行,仍在显示英雄列表,在选择英雄时,仍然会把他/她显示在详情页面中。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
Checkout the "[Take it slow](#slow)" appendix to see what the app might be like with a poor connection.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
查看附件中的“[慢一点儿](#slow)”一节,来了解在较差的网络连接中这个应用会是什么样的。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
### Review the App Structure
|
2016-05-12 17:50:14 +08:00
|
|
|
|
### 回顾本应用的结构
|
2015-12-14 20:05:13 -08:00
|
|
|
|
Let’s verify that we have the following structure after all of our good refactoring in this chapter:
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
再检查下,经历了本章的所有重构之后,我们应该有了下列文件结构:
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
|
|
|
|
.filetree
|
|
|
|
|
.file angular2-tour-of-heroes
|
|
|
|
|
.children
|
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.ts
|
|
|
|
|
.file hero.ts
|
|
|
|
|
.file hero-detail.component.ts
|
|
|
|
|
.file hero.service.ts
|
2016-01-28 16:15:26 -08:00
|
|
|
|
.file main.ts
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.file mock-heroes.ts
|
2016-02-11 15:08:06 -08:00
|
|
|
|
.file node_modules ...
|
|
|
|
|
.file typings ...
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.file index.html
|
|
|
|
|
.file package.json
|
|
|
|
|
.file tsconfig.json
|
2016-02-11 15:08:06 -08:00
|
|
|
|
.file typings.json
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Here are the code files we discussed in this chapter.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
这就是我们在本章讨论过的这些源码文件:
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
+makeTabs(`
|
|
|
|
|
toh-4/ts/app/hero.service.ts,
|
|
|
|
|
toh-4/ts/app/app.component.ts,
|
|
|
|
|
toh-4/ts/app/mock-heroes.ts
|
|
|
|
|
`,'',`
|
|
|
|
|
app/hero.service.ts,
|
|
|
|
|
app/app.component.ts,
|
|
|
|
|
app/mock-heroes.ts
|
|
|
|
|
`)
|
|
|
|
|
:marked
|
|
|
|
|
## The Road We’ve Travelled
|
2016-04-17 10:24:39 +08:00
|
|
|
|
## 走过的路
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Let’s take stock of what we’ve built.
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
来盘点一下我们已经构建完的部分。
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
* We created a service class that can be shared by many components
|
2016-04-17 10:24:39 +08:00
|
|
|
|
* 我们创建了一个能被多个组件共享的服务类
|
2015-12-16 21:47:02 -05:00
|
|
|
|
* We used the `ngOnInit` Lifecycle Hook to get our heroes when our `AppComponent` activates
|
2016-04-17 10:24:39 +08:00
|
|
|
|
* 我们使用`ngOnInit`生命周期钩子,以便在`AppComponent`激活时获取英雄数据。
|
2015-12-16 21:47:02 -05:00
|
|
|
|
* We defined our `HeroService` as a provider for our `AppComponent`
|
2016-05-14 22:23:18 +08:00
|
|
|
|
* 我们把`HeroService`定义为`AppComponent`的一个供应商
|
2015-12-16 21:47:02 -05:00
|
|
|
|
* We created mock hero data and imported them into our service
|
2016-05-12 17:50:14 +08:00
|
|
|
|
* 我们创建了一个模拟的英雄数据,并把它导入我们的服务中
|
2015-12-16 21:47:02 -05:00
|
|
|
|
* We designed our service to return a promise and our component to get our data from the promise
|
2016-05-12 17:50:14 +08:00
|
|
|
|
* 我们把服务改造为返回承诺的,并让组件从承诺获取数据
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2016-06-07 15:51:25 -07:00
|
|
|
|
p Run the #[+liveExampleLink2('', 'toh-4')] for this part.
|
2016-06-08 09:58:16 +08:00
|
|
|
|
|
|
|
|
|
p 运行这部分的#[+liveExampleLink2('在线例子', 'toh-4')]。
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2016-06-07 15:51:25 -07:00
|
|
|
|
:marked
|
2015-12-16 21:47:02 -05:00
|
|
|
|
### The Road Ahead
|
2016-04-17 10:24:39 +08:00
|
|
|
|
### 前方的路
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Our Tour of Heroes has become more reusable using shared components and services.
|
|
|
|
|
We want to create a dashboard, add menu links that route between the views, and format data in a template.
|
|
|
|
|
As our app evolves, we’ll learn how to design it to make it easier to grow and maintain.
|
2016-02-16 18:09:38 +02:00
|
|
|
|
|
2016-05-12 17:50:14 +08:00
|
|
|
|
通过使用共享组件和服务,我们的《英雄指南》更有复用性了。
|
|
|
|
|
我们还要创建一个仪表盘,要添加菜单链接,要路由到各个视图,还要在模板中格式化数据。
|
|
|
|
|
随着我们应用的进化,我们还会学到如何进行设计,让它更易于扩展和维护。
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
2016-02-16 18:09:38 +02:00
|
|
|
|
We learn about Angular Component Router and navigation among the views in the [next tutorial](toh-pt5.html) chapter.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2016-04-17 10:24:39 +08:00
|
|
|
|
我们将在[下一章](toh-pt5.html)学习Angular组件路由,以及在视图间导航的知识。
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.l-main-section
|
|
|
|
|
<a id="slow"></a>
|
|
|
|
|
:marked
|
|
|
|
|
### Appendix: Take it slow
|
2016-04-17 10:24:39 +08:00
|
|
|
|
### 附件:慢一点儿
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
We can simulate a slow connection.
|
|
|
|
|
|
2016-04-17 10:24:39 +08:00
|
|
|
|
我们可以模拟慢速连接。
|
|
|
|
|
|
2016-02-01 10:52:20 -08:00
|
|
|
|
Import the `Hero` symbol and add the following `getHeroesSlowly` method to the `HeroService`
|
2016-04-17 10:24:39 +08:00
|
|
|
|
|
|
|
|
|
导入`Hero`类,并且在`HeroService`中添加如下的`getHeroesSlowly`方法:
|
2016-05-09 19:05:50 +03:00
|
|
|
|
+makeExample('toh-4/ts/app/hero.service.ts', 'get-heroes-slowly', 'hero.service.ts (getHeroesSlowly)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
|
|
|
|
Like `getHeroes`, it also returns a promise.
|
|
|
|
|
But this promise waits 2 seconds before resolving the promise with mock heroes.
|
|
|
|
|
|
2016-04-17 10:24:39 +08:00
|
|
|
|
像`getHeroes`一样,它也返回一个承诺。
|
|
|
|
|
但是,这个承诺会在使用模拟数据完成任务之前等待两秒钟。
|
|
|
|
|
|
2016-05-03 14:06:32 +02:00
|
|
|
|
Back in the `AppComponent`, replace `heroService.getHeroes` with `heroService.getHeroesSlowly`
|
2015-12-16 21:47:02 -05:00
|
|
|
|
and see how the app behaves.
|
2016-06-13 17:22:28 +08:00
|
|
|
|
|
|
|
|
|
回到`AppComponent`,用`heroService.getHeroesSlowly`替换掉`heroService.getHeroes`,并观察本应用的行为。
|
|
|
|
|
|