填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

HTTP

在这节课中,你将借助 Angular 的 HttpClient 来添加一些数据持久化特性。

In this tutorial, you'll add the following data persistence features with help from Angular's HttpClient.

  • HeroService 通过 HTTP 请求获取英雄数据。

    The HeroService gets hero data with HTTP requests.

  • 用户可以添加、编辑和删除英雄,并通过 HTTP 来保存这些更改。

    Users can add, edit, and delete heroes and save these changes over HTTP.

  • 用户可以根据名字搜索英雄。

    Users can search for heroes by name.

当你完成这一章时,应用会变成这样:在线例子 / 下载范例

When you're done with this page, the app should look like this在线例子 / 下载范例.

启用 HTTP 服务

Enable HTTP services

HttpClient 是 Angular 通过 HTTP 与远程服务器通讯的机制。

HttpClient is Angular's mechanism for communicating with a remote server over HTTP.

要让 HttpClient 在应用中随处可用,请

To make HttpClient available everywhere in the app:

import { HttpClientModule } from '@angular/common/http';
src/app/app.module.ts (Http Client import)
      
      import { HttpClientModule }    from '@angular/common/http';
    

模拟数据服务器

Simulate a data server

这个教学例子会与一个使用 内存 Web API(In-memory Web API 模拟出的远程数据服务器通讯。

This tutorial sample mimics communication with a remote data server by using the In-memory Web API module.

安装完这个模块之后,应用将会通过 HttpClient 来发起请求和接收响应,而不用在乎实际上是这个内存 Web API 在拦截这些请求、操作一个内存数据库,并且给出仿真的响应。

After installing the module, the app will make requests to and receive responses from the HttpClient without knowing that the In-memory Web API is intercepting those requests, applying them to an in-memory data store, and returning simulated responses.

这给本教程带来了极大的便利。你不用被迫先架设一个服务器再来学习 HttpClient

This facility is a great convenience for the tutorial. You won't have to set up a server to learn about HttpClient.

在你自己的应用开发的早期阶段这也同样很方便,那时候服务器的 Web API 可能定义上存在错误或者尚未实现。

It may also be convenient in the early stages of your own app development when the server's web api is ill-defined or not yet implemented.

重要: 这个内存 Web API 模块与 Angular 中的 HTTP 模块无关。

Important: the In-memory Web API module has nothing to do with HTTP in Angular.

如果你只是在阅读本教程来学习 HttpClient,那么可以跳过这一步。 如果你正在随着本教程敲代码,那就留下来,并加上这个内存 Web API

If you're just reading this tutorial to learn about HttpClient, you can skip over this step. If you're coding along with this tutorial, stay here and add the In-memory Web API now.

npm 中安装这个内存 Web API 包(译注:请使用 0.5+ 的版本,不要使用 0.4-)

Install the In-memory Web API package from npm

npm install angular-in-memory-web-api --save
      
      npm install angular-in-memory-web-api --save
    

导入 HttpClientInMemoryWebApiModuleInMemoryDataService 类(你很快就要创建它)。

Import the HttpClientInMemoryWebApiModule and the InMemoryDataService class, which you will create in a moment.

import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; import { InMemoryDataService } from './in-memory-data.service';
src/app/app.module.ts (In-memory Web API imports)
      
      import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService }  from './in-memory-data.service';
    

HttpClientInMemoryWebApiModule 添加到 @NgModule.imports 数组中(放在 HttpClient 之后), 然后使用 InMemoryDataService 来配置它。

Add the HttpClientInMemoryWebApiModule to the @NgModule.imports array— after importing the HttpClientModule, —while configuring it with the InMemoryDataService.

HttpClientModule, // The HttpClientInMemoryWebApiModule module intercepts HTTP requests // and returns simulated server responses. // Remove it when a real server is ready to receive requests. HttpClientInMemoryWebApiModule.forRoot( InMemoryDataService, { dataEncapsulation: false } )
      
      HttpClientModule,

// The HttpClientInMemoryWebApiModule module intercepts HTTP requests
// and returns simulated server responses.
// Remove it when a real server is ready to receive requests.
HttpClientInMemoryWebApiModule.forRoot(
  InMemoryDataService, { dataEncapsulation: false }
)
    

forRoot() 配置方法接受一个 InMemoryDataService 类(初期的内存数据库)作为参数。

The forRoot() configuration method takes an InMemoryDataService class that primes the in-memory database.

src/app/in-memory-data.service.ts 类是通过下列命令生成的:

The class src/app/in-memory-data.service.ts is generated by the following command:

ng generate service InMemoryData
      
      ng generate service InMemoryData
    

该类的内容如下:

This class has the following content:

import { InMemoryDbService } from 'angular-in-memory-web-api'; import { Hero } from './hero'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class InMemoryDataService implements InMemoryDbService { createDb() { const heroes = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombasto' }, { id: 14, name: 'Celeritas' }, { id: 15, name: 'Magneta' }, { id: 16, name: 'RubberMan' }, { id: 17, name: 'Dynama' }, { id: 18, name: 'Dr IQ' }, { id: 19, name: 'Magma' }, { id: 20, name: 'Tornado' } ]; return {heroes}; } // Overrides the genId method to ensure that a hero always has an id. // If the heroes array is empty, // the method below returns the initial number (11). // if the heroes array is not empty, the method below returns the highest // hero id + 1. genId(heroes: Hero[]): number { return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11; } }
src/app/in-memory-data.service.ts
      
      import { InMemoryDbService } from 'angular-in-memory-web-api';
import { Hero } from './hero';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class InMemoryDataService implements InMemoryDbService {
  createDb() {
    const heroes = [
      { id: 11, name: 'Mr. Nice' },
      { id: 12, name: 'Narco' },
      { id: 13, name: 'Bombasto' },
      { id: 14, name: 'Celeritas' },
      { id: 15, name: 'Magneta' },
      { id: 16, name: 'RubberMan' },
      { id: 17, name: 'Dynama' },
      { id: 18, name: 'Dr IQ' },
      { id: 19, name: 'Magma' },
      { id: 20, name: 'Tornado' }
    ];
    return {heroes};
  }

  // Overrides the genId method to ensure that a hero always has an id.
  // If the heroes array is empty,
  // the method below returns the initial number (11).
  // if the heroes array is not empty, the method below returns the highest
  // hero id + 1.
  genId(heroes: Hero[]): number {
    return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11;
  }
}
    

这个文件替代了 mock-heroes.ts(你可以安全删除它了)。

This file replaces mock-heroes.ts, which is now safe to delete.

等你真实的服务器就绪时,就可以删除这个内存 Web API,该应用的请求就会直接发给真实的服务器。

When your server is ready, detach the In-memory Web API, and the app's requests will go through to the server.

现在,回来看 HttpClient

Now back to the HttpClient story.

英雄与 HTTP

Heroes and HTTP

导入一些所需的 HTTP 符号:

Import some HTTP symbols that you'll need:

import { HttpClient, HttpHeaders } from '@angular/common/http';
src/app/hero.service.ts (import HTTP symbols)
      
      import { HttpClient, HttpHeaders } from '@angular/common/http';
    

HttpClient 注入到构造函数中一个名叫 http 的私有属性中。

Inject HttpClient into the constructor in a private property called http.

constructor( private http: HttpClient, private messageService: MessageService) { }
      
      constructor(
  private http: HttpClient,
  private messageService: MessageService) { }
    

保留对 MessageService 的注入。你将会频繁调用它,因此请把它包裹进一个私有的 log 方法中。

Keep injecting the MessageService. You'll call it so frequently that you'll wrap it in a private log() method.

/** Log a HeroService message with the MessageService */ private log(message: string) { this.messageService.add(`HeroService: ${message}`); }
      
      /** Log a HeroService message with the MessageService */
private log(message: string) {
  this.messageService.add(`HeroService: ${message}`);
}
    

把服务器上英雄数据资源的访问地址 heroesURL 定义为 :base/:collectionName 的形式。 这里的 base 是要请求的资源,而 collectionNamein-memory-data-service.ts 中的英雄数据对象。

Define the heroesUrl of the form :base/:collectionName with the address of the heroes resource on the server. Here base is the resource to which requests are made, and collectionName is the heroes data object in the in-memory-data-service.ts.

private heroesUrl = 'api/heroes'; // URL to web api
      
      private heroesUrl = 'api/heroes';  // URL to web api
    

通过 HttpClient 获取英雄

Get heroes with HttpClient

当前的 HeroService.getHeroes() 使用 RxJS 的 of() 函数来把模拟英雄数据返回为 Observable<Hero[]> 格式。

The current HeroService.getHeroes() uses the RxJS of() function to return an array of mock heroes as an Observable<Hero[]>.

getHeroes(): Observable<Hero[]> { return of(HEROES); }
src/app/hero.service.ts (getHeroes with RxJs 'of()')
      
      getHeroes(): Observable<Hero[]> {
  return of(HEROES);
}
    

把该方法转换成使用 HttpClient

Convert that method to use HttpClient

/** GET heroes from the server */ getHeroes (): Observable<Hero[]> { return this.http.get<Hero[]>(this.heroesUrl) }
      
      /** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
  return this.http.get<Hero[]>(this.heroesUrl)
}
    

刷新浏览器后,英雄数据就会从模拟服务器被成功读取。

Refresh the browser. The hero data should successfully load from the mock server.

你用 http.get 替换了 of,没有做其它修改,但是应用仍然在正常工作,这是因为这两个函数都返回了 Observable<Hero[]>

You've swapped of for http.get and the app keeps working without any other changes because both functions return an Observable<Hero[]>.

Http 方法返回单个值

Http methods return one value

所有的 HttpClient 方法都会返回某个值的 RxJS Observable

All HttpClient methods return an RxJS Observable of something.

HTTP 是一个请求/响应式协议。你发起请求,它返回单个的响应。

HTTP is a request/response protocol. You make a request, it returns a single response.

通常,Observable 可以在一段时间内返回多个值。 但来自 HttpClientObservable 总是发出一个值,然后结束,再也不会发出其它值。

In general, an observable can return multiple values over time. An observable from HttpClient always emits a single value and then completes, never to emit again.

具体到这次 HttpClient.get 调用,它返回一个 Observable<Hero[]>,顾名思义就是“一个英雄数组的可观察对象”。在实践中,它也只会返回一个英雄数组。

This particular HttpClient.get call returns an Observable<Hero[]>, literally "an observable of hero arrays". In practice, it will only return a single hero array.

HttpClient.get 返回响应数据

HttpClient.get returns response data

HttpClient.get 默认情况下把响应体当做无类型的 JSON 对象进行返回。 如果指定了可选的模板类型 <Hero[]>,就会给返回你一个类型化的对象。

HttpClient.get returns the body of the response as an untyped JSON object by default. Applying the optional type specifier, <Hero[]> , gives you a typed result object.

JSON 数据的具体形态是由服务器的数据 API 决定的。 英雄指南的数据 API 会把英雄数据作为一个数组进行返回。

The shape of the JSON data is determined by the server's data API. The Tour of Heroes data API returns the hero data as an array.

其它 API 可能在返回对象中深埋着你想要的数据。 你可能要借助 RxJS 的 map 操作符对 Observable 的结果进行处理,以便把这些数据挖掘出来。

Other APIs may bury the data that you want within an object. You might have to dig that data out by processing the Observable result with the RxJS map operator.

虽然不打算在此展开讨论,不过你可以到范例源码中的 getHeroNo404() 方法中找到一个使用 map 操作符的例子。

Although not discussed here, there's an example of map in the getHeroNo404() method included in the sample source code.

错误处理

Error handling

凡事皆会出错,特别是当你从远端服务器获取数据的时候。 HeroService.getHeroes() 方法应该捕获错误,并做适当的处理。

Things go wrong, especially when you're getting data from a remote server. The HeroService.getHeroes() method should catch errors and do something appropriate.

要捕获错误,你就要使用 RxJS 的 catchError() 操作符来建立对 Observable 结果的处理管道(pipe)

To catch errors, you "pipe" the observable result from http.get() through an RxJS catchError() operator.

rxjs/operators 中导入 catchError 符号,以及你稍后将会用到的其它操作符。

Import the catchError symbol from rxjs/operators, along with some other operators you'll need later.

import { catchError, map, tap } from 'rxjs/operators';
      
      import { catchError, map, tap } from 'rxjs/operators';
    

现在,使用 .pipe() 方法来扩展 Observable 的结果,并给它一个 catchError() 操作符。

Now extend the observable result with the .pipe() method and give it a catchError() operator.

getHeroes (): Observable<Hero[]> { return this.http.get<Hero[]>(this.heroesUrl) .pipe( catchError(this.handleError<Hero[]>('getHeroes', [])) ); }
      
      getHeroes (): Observable<Hero[]> {
  return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
      catchError(this.handleError<Hero[]>('getHeroes', []))
    );
}
    

catchError() 操作符会拦截失败的 Observable。 它把错误对象传给错误处理器错误处理器会处理这个错误。

The catchError() operator intercepts an Observable that failed. It passes the error an error handler that can do what it wants with the error.

下面的 handleError() 方法会报告这个错误,并返回一个无害的结果(安全值),以便应用能正常工作。

The following handleError() method reports the error and then returns an innocuous result so that the application keeps working.

handleError

下面这个 handleError() 将会在很多 HeroService 的方法之间共享,所以要把它通用化,以支持这些彼此不同的需求。

The following handleError() will be shared by many HeroService methods so it's generalized to meet their different needs.

它不再直接处理这些错误,而是返回给 catchError 返回一个错误处理函数。还要用操作名和出错时要返回的安全值来对这个错误处理函数进行配置。

Instead of handling the error directly, it returns an error handler function to catchError that it has configured with both the name of the operation that failed and a safe return value.

/** * Handle Http operation that failed. * Let the app continue. * @param operation - name of the operation that failed * @param result - optional value to return as the observable result */ private handleError<T> (operation = 'operation', result?: T) { return (error: any): Observable<T> => { // TODO: send the error to remote logging infrastructure console.error(error); // log to console instead // TODO: better job of transforming error for user consumption this.log(`${operation} failed: ${error.message}`); // Let the app keep running by returning an empty result. return of(result as T); }; }
      
      
  1. /**
  2. * Handle Http operation that failed.
  3. * Let the app continue.
  4. * @param operation - name of the operation that failed
  5. * @param result - optional value to return as the observable result
  6. */
  7. private handleError<T> (operation = 'operation', result?: T) {
  8. return (error: any): Observable<T> => {
  9.  
  10. // TODO: send the error to remote logging infrastructure
  11. console.error(error); // log to console instead
  12.  
  13. // TODO: better job of transforming error for user consumption
  14. this.log(`${operation} failed: ${error.message}`);
  15.  
  16. // Let the app keep running by returning an empty result.
  17. return of(result as T);
  18. };
  19. }

在控制台中汇报了这个错误之后,这个处理器会汇报一个用户友好的消息,并给应用返回一个安全值,让它继续工作。

After reporting the error to console, the handler constructs a user friendly message and returns a safe value to the app so it can keep working.

因为每个服务方法都会返回不同类型的 Observable 结果,因此 handleError() 也需要一个类型参数,以便它返回一个此类型的安全值,正如应用所期望的那样。

Because each service method returns a different kind of Observable result, handleError() takes a type parameter so it can return the safe value as the type that the app expects.

窥探 Observable

Tap into the Observable

HeroService 的方法将会窥探 Observable 的数据流,并通过 log() 函数往页面底部发送一条消息。

The HeroService methods will tap into the flow of observable values and send a message (via log()) to the message area at the bottom of the page.

它们可以使用 RxJS 的 tap 操作符来实现,该操作符会查看 Observable 中的值,使用那些值做一些事情,并且把它们传出来。 这种 tap 回调不会改变这些值本身。

They'll do that with the RxJS tap operator, which looks at the observable values, does something with those values, and passes them along. The tap call back doesn't touch the values themselves.

下面是 getHeroes 的最终版本,它使用 tap 来记录各种操作。

Here is the final version of getHeroes with the tap that logs the operation.

/** GET heroes from the server */ getHeroes (): Observable<Hero[]> { return this.http.get<Hero[]>(this.heroesUrl) .pipe( tap(_ => this.log('fetched heroes')), catchError(this.handleError<Hero[]>('getHeroes', [])) ); }
      
      /** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
  return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
      tap(_ => this.log('fetched heroes')),
      catchError(this.handleError<Hero[]>('getHeroes', []))
    );
}
    

通过 id 获取英雄

Get hero by id

大多数的 Web API 都支持以 :baseURL/:id 的形式根据 id 进行获取。

Most web APIs support a get by id request in the form :baseURL/:id.

这里的 baseURL 就是在 英雄列表与 HTTP 部分定义过的 heroesURLapi/heroes)。而 id 则是你要获取的英雄的编号,比如,api/heroes/11。 添加一个 HeroService.getHero() 方法,以发起该请求:

Here, the base URL is the heroesURL defined in the Heroes and HTTP section (api/heroes) and id is the number of the hero that you want to retrieve. For example, api/heroes/11. Add a HeroService.getHero() method to make that request:

/** GET hero by id. Will 404 if id not found */ getHero(id: number): Observable<Hero> { const url = `${this.heroesUrl}/${id}`; return this.http.get<Hero>(url).pipe( tap(_ => this.log(`fetched hero id=${id}`)), catchError(this.handleError<Hero>(`getHero id=${id}`)) ); }
src/app/hero.service.ts
      
      /** GET hero by id. Will 404 if id not found */
getHero(id: number): Observable<Hero> {
  const url = `${this.heroesUrl}/${id}`;
  return this.http.get<Hero>(url).pipe(
    tap(_ => this.log(`fetched hero id=${id}`)),
    catchError(this.handleError<Hero>(`getHero id=${id}`))
  );
}
    

这里和 getHeroes() 相比有三个显著的差异。

There are three significant differences from getHeroes().

  • 它使用想获取的英雄的 id 构建了一个请求 URL。

    it constructs a request URL with the desired hero's id.

  • 服务器应该使用单个英雄作为回应,而不是一个英雄数组。

    the server should respond with a single hero rather than an array of heroes.

  • 所以,getHero 会返回 Observable<Hero>(“一个可观察的单个英雄对象”),而不是一个可观察的英雄对象数组

    therefore, getHero returns an Observable<Hero> ("an observable of Hero objects") rather than an observable of hero arrays .

修改英雄

Update heroes

英雄详情视图中编辑英雄的名字。 随着输入,英雄的名字也跟着在页面顶部的标题区更新了。 但是当你点击“后退”按钮时,这些修改都丢失了。

Edit a hero's name in the hero detail view. As you type, the hero name updates the heading at the top of the page. But when you click the "go back button", the changes are lost.

如果你希望保留这些修改,就要把它们写回到服务器。

If you want changes to persist, you must write them back to the server.

在英雄详情模板的底部添加一个保存按钮,它绑定了一个 click 事件,事件绑定会调用组件中一个名叫 save() 的新方法:

At the end of the hero detail template, add a save button with a click event binding that invokes a new component method named save().

<button (click)="save()">save</button>
src/app/hero-detail/hero-detail.component.html (save)
      
      <button (click)="save()">save</button>
    

添加如下的 save() 方法,它使用英雄服务中的 updateHero() 方法来保存对英雄名字的修改,然后导航回前一个视图。

Add the following save() method, which persists hero name changes using the hero service updateHero() method and then navigates back to the previous view.

save(): void { this.heroService.updateHero(this.hero) .subscribe(() => this.goBack()); }
src/app/hero-detail/hero-detail.component.ts (save)
      
      save(): void {
   this.heroService.updateHero(this.hero)
     .subscribe(() => this.goBack());
 }
    

添加 HeroService.updateHero()

Add HeroService.updateHero()

updateHero() 的总体结构和 getHeroes() 很相似,但它会使用 http.put() 来把修改后的英雄保存到服务器上。

The overall structure of the updateHero() method is similar to that of getHeroes(), but it uses http.put() to persist the changed hero on the server.

/** PUT: update the hero on the server */ updateHero (hero: Hero): Observable<any> { return this.http.put(this.heroesUrl, hero, httpOptions).pipe( tap(_ => this.log(`updated hero id=${hero.id}`)), catchError(this.handleError<any>('updateHero')) ); }
src/app/hero.service.ts (update)
      
      /** PUT: update the hero on the server */
updateHero (hero: Hero): Observable<any> {
  return this.http.put(this.heroesUrl, hero, httpOptions).pipe(
    tap(_ => this.log(`updated hero id=${hero.id}`)),
    catchError(this.handleError<any>('updateHero'))
  );
}
    

HttpClient.put() 方法接受三个参数

The HttpClient.put() method takes three parameters

  • URL 地址

    the URL

  • 要修改的数据(这里就是修改后的英雄)

    the data to update (the modified hero in this case)

  • 选项

    options

URL 没变。英雄 Web API 通过英雄对象的 id 就可以知道要修改哪个英雄。

The URL is unchanged. The heroes web API knows which hero to update by looking at the hero's id.

英雄 Web API 期待在保存时的请求中有一个特殊的头。 这个头是在 HeroServicehttpOptions 常量中定义的。

The heroes web API expects a special header in HTTP save requests. That header is in the httpOptions constant defined in the HeroService.

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
src/app/hero.service.ts
      
      const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
    

刷新浏览器,修改英雄名,保存这些修改。在 HeroDetailComponentsave() 方法中实现了 "导航到前一个视图" 的逻辑。 现在,改名后的英雄已经显示在列表中了。

Refresh the browser, change a hero name and save your change. Navigating to the previous view is implemented in the save() method defined in HeroDetailComponent. The hero now appears in the list with the changed name.

添加新英雄

Add a new hero

要添加英雄,本应用中只需要英雄的名字。你可以使用一个和添加按钮成对的 input 元素。

To add a hero, this app only needs the hero's name. You can use an input element paired with an add button.

把下列代码插入到 HeroesComponent 模板中标题的紧后面:

Insert the following into the HeroesComponent template, just after the heading:

<div> <label>Hero name: <input #heroName /> </label> <!-- (click) passes input value to add() and then clears the input --> <button (click)="add(heroName.value); heroName.value=''"> add </button> </div>
src/app/heroes/heroes.component.html (add)
      
      <div>
  <label>Hero name:
    <input #heroName />
  </label>
  <!-- (click) passes input value to add() and then clears the input -->
  <button (click)="add(heroName.value); heroName.value=''">
    add
  </button>
</div>
    

当点击事件触发时,调用组件的点击处理器,然后清空这个输入框,以便用来输入另一个名字。

In response to a click event, call the component's click handler and then clear the input field so that it's ready for another name.

add(name: string): void { name = name.trim(); if (!name) { return; } this.heroService.addHero({ name } as Hero) .subscribe(hero => { this.heroes.push(hero); }); }
src/app/heroes/heroes.component.ts (add)
      
      add(name: string): void {
  name = name.trim();
  if (!name) { return; }
  this.heroService.addHero({ name } as Hero)
    .subscribe(hero => {
      this.heroes.push(hero);
    });
}
    

当指定的名字非空时,这个处理器会用这个名字创建一个类似于 Hero 的对象(只缺少 id 属性),并把它传给服务的 addHero() 方法。

When the given name is non-blank, the handler creates a Hero-like object from the name (it's only missing the id) and passes it to the services addHero() method.

addHero 保存成功时,subscribe 的回调函数会收到这个新英雄,并把它追加到 heroes 列表中以供显示。

When addHero saves successfully, the subscribe callback receives the new hero and pushes it into to the heroes list for display.

你将在下一节编写 HeroService.addHero

You'll write HeroService.addHero in the next section.

添加 HeroService.addHero()

Add HeroService.addHero()

HeroService 类中添加 addHero() 方法。

Add the following addHero() method to the HeroService class.

/** POST: add a new hero to the server */ addHero (hero: Hero): Observable<Hero> { return this.http.post<Hero>(this.heroesUrl, hero, httpOptions).pipe( tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)), catchError(this.handleError<Hero>('addHero')) ); }
src/app/hero.service.ts (addHero)
      
      /** POST: add a new hero to the server */
addHero (hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, httpOptions).pipe(
    tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),
    catchError(this.handleError<Hero>('addHero'))
  );
}
    

HeroService.addHero()updateHero 有两点不同。

HeroService.addHero() differs from updateHero in two ways.

  • 它调用 HttpClient.post() 而不是 put()

    it calls HttpClient.post() instead of put().

  • 它期待服务器为这个新的英雄生成一个 id,然后把它通过 Observable<Hero> 返回给调用者。

    it expects the server to generate an id for the new hero, which it returns in the Observable<Hero> to the caller.

刷新浏览器,并添加一些英雄。

Refresh the browser and add some heroes.

删除某个英雄

Delete a hero

英雄列表中的每个英雄都有一个删除按钮。

Each hero in the heroes list should have a delete button.

把下列按钮(button)元素添加到 HeroesComponent 的模板中,就在每个 <li> 元素中的英雄名字后方。

Add the following button element to the HeroesComponent template, after the hero name in the repeated <li> element.

<button class="delete" title="delete hero" (click)="delete(hero)">x</button>
      
      <button class="delete" title="delete hero"
  (click)="delete(hero)">x</button>
    

英雄列表的 HTML 应该是这样的:

The HTML for the list of heroes should look like this:

<ul class="heroes"> <li *ngFor="let hero of heroes"> <a routerLink="/detail/{{hero.id}}"> <span class="badge">{{hero.id}}</span> {{hero.name}} </a> <button class="delete" title="delete hero" (click)="delete(hero)">x</button> </li> </ul>
src/app/heroes/heroes.component.html (list of heroes)
      
      <ul class="heroes">
  <li *ngFor="let hero of heroes">
    <a routerLink="/detail/{{hero.id}}">
      <span class="badge">{{hero.id}}</span> {{hero.name}}
    </a>
    <button class="delete" title="delete hero"
      (click)="delete(hero)">x</button>
  </li>
</ul>
    

要把删除按钮定位在每个英雄条目的最右边,就要往 heroes.component.css 中添加一些 CSS。你可以在下方的 最终代码 中找到这些 CSS。

To position the delete button at the far right of the hero entry, add some CSS to the heroes.component.css. You'll find that CSS in the final review code below.

delete() 处理器添加到组件中。

Add the delete() handler to the component.

delete(hero: Hero): void { this.heroes = this.heroes.filter(h => h !== hero); this.heroService.deleteHero(hero).subscribe(); }
src/app/heroes/heroes.component.ts (delete)
      
      delete(hero: Hero): void {
  this.heroes = this.heroes.filter(h => h !== hero);
  this.heroService.deleteHero(hero).subscribe();
}
    

虽然这个组件把删除英雄的逻辑委托给了 HeroService,但扔保留了更新它自己的英雄列表的职责。 组件的 delete() 方法会在 HeroService 对服务器的操作成功之前,先从列表中移除要删除的英雄

Although the component delegates hero deletion to the HeroService, it remains responsible for updating its own list of heroes. The component's delete() method immediately removes the hero-to-delete from that list, anticipating that the HeroService will succeed on the server.

组件与 heroService.delete() 返回的 Observable 还完全没有关联。必须订阅它

There's really nothing for the component to do with the Observable returned by heroService.delete(). It must subscribe anyway.

如果你忘了调用 subscribe(),本服务将不会把这个删除请求发送给服务器。 作为一条通用的规则,Observable 在有人订阅之前什么都不会做

If you neglect to subscribe(), the service will not send the delete request to the server! As a rule, an Observable does nothing until something subscribes!

你可以暂时删除 subscribe() 来确认这一点。点击“Dashboard”,然后点击“Heroes”,就又看到完整的英雄列表了。

Confirm this for yourself by temporarily removing the subscribe(), clicking "Dashboard", then clicking "Heroes". You'll see the full list of heroes again.

添加 HeroService.deleteHero()

Add HeroService.deleteHero()

deleteHero() 方法添加到 HeroService 中,代码如下。

Add a deleteHero() method to HeroService like this.

/** DELETE: delete the hero from the server */ deleteHero (hero: Hero | number): Observable<Hero> { const id = typeof hero === 'number' ? hero : hero.id; const url = `${this.heroesUrl}/${id}`; return this.http.delete<Hero>(url, httpOptions).pipe( tap(_ => this.log(`deleted hero id=${id}`)), catchError(this.handleError<Hero>('deleteHero')) ); }
src/app/hero.service.ts (delete)
      
      /** DELETE: delete the hero from the server */
deleteHero (hero: Hero | number): Observable<Hero> {
  const id = typeof hero === 'number' ? hero : hero.id;
  const url = `${this.heroesUrl}/${id}`;

  return this.http.delete<Hero>(url, httpOptions).pipe(
    tap(_ => this.log(`deleted hero id=${id}`)),
    catchError(this.handleError<Hero>('deleteHero'))
  );
}
    

注意

Note that

  • 它调用了 HttpClient.delete

    it calls HttpClient.delete.

  • URL 就是英雄的资源 URL 加上要删除的英雄的 id

    the URL is the heroes resource URL plus the id of the hero to delete

  • 你不用像 putpost 中那样发送任何数据。

    you don't send data as you did with put and post.

  • 你仍要发送 httpOptions

    you still send the httpOptions.

刷新浏览器,并试一下这个新的删除功能。

Refresh the browser and try the new delete functionality.

根据名字搜索

Search by name

在最后一次练习中,你要学到把 Observable 的操作符串在一起,让你能将相似 HTTP 请求的数量最小化,并节省网络带宽。

In this last exercise, you learn to chain Observable operators together so you can minimize the number of similar HTTP requests and consume network bandwidth economically.

你将往仪表盘中加入英雄搜索特性。 当用户在搜索框中输入名字时,你会不断发送根据名字过滤英雄的 HTTP 请求。 你的目标是仅仅发出尽可能少的必要请求。

You will add a heroes search feature to the Dashboard. As the user types a name into a search box, you'll make repeated HTTP requests for heroes filtered by that name. Your goal is to issue only as many requests as necessary.

HeroService.searchHeroes

先把 searchHeroes 方法添加到 HeroService 中。

Start by adding a searchHeroes method to the HeroService.

/* GET heroes whose name contains search term */ searchHeroes(term: string): Observable<Hero[]> { if (!term.trim()) { // if not search term, return empty hero array. return of([]); } return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe( tap(_ => this.log(`found heroes matching "${term}"`)), catchError(this.handleError<Hero[]>('searchHeroes', [])) ); }
src/app/hero.service.ts
      
      /* GET heroes whose name contains search term */
searchHeroes(term: string): Observable<Hero[]> {
  if (!term.trim()) {
    // if not search term, return empty hero array.
    return of([]);
  }
  return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe(
    tap(_ => this.log(`found heroes matching "${term}"`)),
    catchError(this.handleError<Hero[]>('searchHeroes', []))
  );
}
    

如果没有搜索词,该方法立即返回一个空数组。 剩下的部分和 getHeroes() 很像。 唯一的不同点是 URL,它包含了一个由搜索词组成的查询字符串。

The method returns immediately with an empty array if there is no search term. The rest of it closely resembles getHeroes(). The only significant difference is the URL, which includes a query string with the search term.

为仪表盘添加搜索功能

Add search to the Dashboard

打开 DashboardComponent模板并且把用于搜索英雄的元素 <app-hero-search> 添加到 DashboardComponent 模板的底部。

Open the DashboardComponent template and Add the hero search element, <app-hero-search>, to the bottom of the DashboardComponent template.

<h3>Top Heroes</h3> <div class="grid grid-pad"> <a *ngFor="let hero of heroes" class="col-1-4" routerLink="/detail/{{hero.id}}"> <div class="module hero"> <h4>{{hero.name}}</h4> </div> </a> </div> <app-hero-search></app-hero-search>
src/app/dashboard/dashboard.component.html
      
      <h3>Top Heroes</h3>
<div class="grid grid-pad">
  <a *ngFor="let hero of heroes" class="col-1-4"
      routerLink="/detail/{{hero.id}}">
    <div class="module hero">
      <h4>{{hero.name}}</h4>
    </div>
  </a>
</div>

<app-hero-search></app-hero-search>
    

这个模板看起来很像 HeroesComponent 模板中的 *ngFor 复写器。

This template looks a lot like the *ngFor repeater in the HeroesComponent template.

很不幸,添加这个元素让本应用挂了。 Angular 找不到哪个组件的选择器能匹配上 <app-hero-search>

Unfortunately, adding this element breaks the app. Angular can't find a component with a selector that matches <app-hero-search>.

HeroSearchComponent 还不存在,这就解决。

The HeroSearchComponent doesn't exist yet. Fix that.

创建 HeroSearchComponent

Create HeroSearchComponent

使用 CLI 创建一个 HeroSearchComponent

Create a HeroSearchComponent with the CLI.

ng generate component hero-search
      
      ng generate component hero-search
    

CLI 生成了 HeroSearchComponent 的三个文件,并把该组件添加到了 AppModule 的声明中。

The CLI generates the three HeroSearchComponent files and adds the component to the AppModule declarations

把生成的 HeroSearchComponent模板改成一个输入框和一个匹配到的搜索结果的列表。代码如下:

Replace the generated HeroSearchComponent template with a text box and a list of matching search results like this.

<div id="search-component"> <h4>Hero Search</h4> <input #searchBox id="search-box" (input)="search(searchBox.value)" /> <ul class="search-result"> <li *ngFor="let hero of heroes$ | async" > <a routerLink="/detail/{{hero.id}}"> {{hero.name}} </a> </li> </ul> </div>
src/app/hero-search/hero-search.component.html
      
      
  1. <div id="search-component">
  2. <h4>Hero Search</h4>
  3.  
  4. <input #searchBox id="search-box" (input)="search(searchBox.value)" />
  5.  
  6. <ul class="search-result">
  7. <li *ngFor="let hero of heroes$ | async" >
  8. <a routerLink="/detail/{{hero.id}}">
  9. {{hero.name}}
  10. </a>
  11. </li>
  12. </ul>
  13. </div>

从下面的 最终代码 中把私有 CSS 样式添加到 hero-search.component.css 中。

Add private CSS styles to hero-search.component.css as listed in the final code review below.

当用户在搜索框中输入时,一个 keyup 事件绑定会调用该组件的 search() 方法,并传入新的搜索框的值。

As the user types in the search box, an input event binding calls the component's search() method with the new search box value.

AsyncPipe

如你所愿,*ngFor 重复渲染出了这些英雄。

As expected, the *ngFor repeats hero objects.

仔细看,你会发现 *ngFor 是在一个名叫 heroes$ 的列表上迭代,而不是 heroes

Look closely and you'll see that the *ngFor iterates over a list called heroes$, not heroes.

<li *ngFor="let hero of heroes$ | async" >
      
      <li *ngFor="let hero of heroes$ | async" >
    

$ 是一个命名惯例,用来表明 heroes$ 是一个 Observable,而不是数组。

The $ is a convention that indicates heroes$ is an Observable, not an array.

*ngFor 不能直接使用 Observable。 不过,它后面还有一个管道字符(|),后面紧跟着一个 async,它表示 Angular 的 AsyncPipe

The *ngFor can't do anything with an Observable. But there's also a pipe character (|) followed by async, which identifies Angular's AsyncPipe.

AsyncPipe 会自动订阅到 Observable,这样你就不用再在组件类中订阅了。

The AsyncPipe subscribes to an Observable automatically so you won't have to do so in the component class.

修正 HeroSearchComponent

Fix the HeroSearchComponent class

修改所生成的 HeroSearchComponent 类及其元数据,代码如下:

Replace the generated HeroSearchComponent class and metadata as follows.

import { Component, OnInit } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Hero } from '../hero'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-hero-search', templateUrl: './hero-search.component.html', styleUrls: [ './hero-search.component.css' ] }) export class HeroSearchComponent implements OnInit { heroes$: Observable<Hero[]>; private searchTerms = new Subject<string>(); constructor(private heroService: HeroService) {} // Push a search term into the observable stream. search(term: string): void { this.searchTerms.next(term); } ngOnInit(): void { this.heroes$ = this.searchTerms.pipe( // wait 300ms after each keystroke before considering the term debounceTime(300), // ignore new term if same as previous term distinctUntilChanged(), // switch to new search observable each time the term changes switchMap((term: string) => this.heroService.searchHeroes(term)), ); } }
src/app/hero-search/hero-search.component.ts
      
      
  1. import { Component, OnInit } from '@angular/core';
  2.  
  3. import { Observable, Subject } from 'rxjs';
  4.  
  5. import {
  6. debounceTime, distinctUntilChanged, switchMap
  7. } from 'rxjs/operators';
  8.  
  9. import { Hero } from '../hero';
  10. import { HeroService } from '../hero.service';
  11.  
  12. @Component({
  13. selector: 'app-hero-search',
  14. templateUrl: './hero-search.component.html',
  15. styleUrls: [ './hero-search.component.css' ]
  16. })
  17. export class HeroSearchComponent implements OnInit {
  18. heroes$: Observable<Hero[]>;
  19. private searchTerms = new Subject<string>();
  20.  
  21. constructor(private heroService: HeroService) {}
  22.  
  23. // Push a search term into the observable stream.
  24. search(term: string): void {
  25. this.searchTerms.next(term);
  26. }
  27.  
  28. ngOnInit(): void {
  29. this.heroes$ = this.searchTerms.pipe(
  30. // wait 300ms after each keystroke before considering the term
  31. debounceTime(300),
  32.  
  33. // ignore new term if same as previous term
  34. distinctUntilChanged(),
  35.  
  36. // switch to new search observable each time the term changes
  37. switchMap((term: string) => this.heroService.searchHeroes(term)),
  38. );
  39. }
  40. }

注意,heroes$ 声明为一个 Observable

Notice the declaration of heroes$ as an Observable

heroes$: Observable<Hero[]>;
      
      heroes$: Observable<Hero[]>;
    

你将会在 ngOnInit()中设置它,在此之前,先仔细看看 searchTerms 的定义。

You'll set it in ngOnInit(). Before you do, focus on the definition of searchTerms.

RxJS Subject 类型的 searchTerms

The searchTerms RxJS subject

searchTerms 属性声明成了 RxJS 的 Subject 类型。

The searchTerms property is declared as an RxJS Subject.

private searchTerms = new Subject<string>(); // Push a search term into the observable stream. search(term: string): void { this.searchTerms.next(term); }
      
      private searchTerms = new Subject<string>();

// Push a search term into the observable stream.
search(term: string): void {
  this.searchTerms.next(term);
}
    

Subject 既是可观察对象的数据源,本身也是 Observable。 你可以像订阅任何 Observable 一样订阅 Subject

A Subject is both a source of observable values and an Observable itself. You can subscribe to a Subject as you would any Observable.

你还可以通过调用它的 next(value) 方法往 Observable 中推送一些值,就像 search() 方法中一样。

You can also push values into that Observable by calling its next(value) method as the search() method does.

search() 是通过对文本框的 keystroke 事件的事件绑定来调用的。

The search() method is called via an event binding to the textbox's input event.

<input #searchBox id="search-box" (input)="search(searchBox.value)" />
      
      <input #searchBox id="search-box" (input)="search(searchBox.value)" />
    

每当用户在文本框中输入时,这个事件绑定就会使用文本框的值(搜索词)调用 search() 函数。 searchTerms 变成了一个能发出搜索词的稳定的流。

Every time the user types in the textbox, the binding calls search() with the textbox value, a "search term". The searchTerms becomes an Observable emitting a steady stream of search terms.

串联 RxJS 操作符

Chaining RxJS operators

如果每当用户击键后就直接调用 searchHeroes() 将导致创建海量的 HTTP 请求,浪费服务器资源并消耗大量网络流量。

Passing a new search term directly to the searchHeroes() after every user keystroke would create an excessive amount of HTTP requests, taxing server resources and burning through the cellular network data plan.

应该怎么做呢?ngOnInit()searchTerms 这个可观察对象的处理管道中加入了一系列 RxJS 操作符,用以缩减对 searchHeroes() 的调用次数,并最终返回一个可及时给出英雄搜索结果的可观察对象(每次都是 Hero[] )。

Instead, the ngOnInit() method pipes the searchTerms observable through a sequence of RxJS operators that reduce the number of calls to the searchHeroes(), ultimately returning an observable of timely hero search results (each a Hero[]).

代码如下:

Here's the code.

this.heroes$ = this.searchTerms.pipe( // wait 300ms after each keystroke before considering the term debounceTime(300), // ignore new term if same as previous term distinctUntilChanged(), // switch to new search observable each time the term changes switchMap((term: string) => this.heroService.searchHeroes(term)), );
      
      this.heroes$ = this.searchTerms.pipe(
  // wait 300ms after each keystroke before considering the term
  debounceTime(300),

  // ignore new term if same as previous term
  distinctUntilChanged(),

  // switch to new search observable each time the term changes
  switchMap((term: string) => this.heroService.searchHeroes(term)),
);
    
  • 在传出最终字符串之前,debounceTime(300) 将会等待,直到新增字符串的事件暂停了 300 毫秒。 你实际发起请求的间隔永远不会小于 300ms。

    debounceTime(300) waits until the flow of new string events pauses for 300 milliseconds before passing along the latest string. You'll never make requests more frequently than 300ms.

  • distinctUntilChanged() 会确保只在过滤条件变化时才发送请求。

    distinctUntilChanged() ensures that a request is sent only if the filter text changed.

  • switchMap() 会为每个从 debouncedistinctUntilChanged 中通过的搜索词调用搜索服务。 它会取消并丢弃以前的搜索可观察对象,只保留最近的。

    switchMap() calls the search service for each search term that makes it through debounce and distinctUntilChanged. It cancels and discards previous search observables, returning only the latest search service observable.

借助 switchMap 操作符, 每个有效的击键事件都会触发一次 HttpClient.get() 方法调用。 即使在每个请求之间都有至少 300ms 的间隔,仍然可能会同时存在多个尚未返回的 HTTP 请求。

With the switchMap operator, every qualifying key event can trigger an HttpClient.get() method call. Even with a 300ms pause between requests, you could have multiple HTTP requests in flight and they may not return in the order sent.

switchMap() 会记住原始的请求顺序,只会返回最近一次 HTTP 方法调用的结果。 以前的那些请求都会被取消和舍弃。

switchMap() preserves the original request order while returning only the observable from the most recent HTTP method call. Results from prior calls are canceled and discarded.

注意,取消前一个 searchHeroes() 可观察对象并不会中止尚未完成的 HTTP 请求。 那些不想要的结果只会在它们抵达应用代码之前被舍弃。

Note that canceling a previous searchHeroes() Observable doesn't actually abort a pending HTTP request. Unwanted results are simply discarded before they reach your application code.

记住,组件类中并没有订阅 heroes$ 这个可观察对象,而是由模板中的 AsyncPipe完成的。

Remember that the component class does not subscribe to the heroes$ observable. That's the job of the AsyncPipein the template.

试试看

Try it

再次运行本应用。在这个 仪表盘 中,在搜索框中输入一些文字。如果你输入的字符匹配上了任何现有英雄的名字,你将会看到如下效果:

Run the app again. In the Dashboard, enter some text in the search box. If you enter characters that match any existing hero names, you'll see something like this.

Hero Search Component

查看最终代码

Final code review

你的应用现在变成了这样:在线例子 / 下载范例

Your app should look like this在线例子 / 下载范例.

本文讨论过的代码文件如下(都位于 src/app/ 文件夹中)。

Here are the code files discussed on this page (all in the src/app/ folder).

HeroService, InMemoryDataService, AppModule

import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable, of } from 'rxjs'; import { catchError, map, tap } from 'rxjs/operators'; import { Hero } from './hero'; import { MessageService } from './message.service'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; @Injectable({ providedIn: 'root' }) export class HeroService { private heroesUrl = 'api/heroes'; // URL to web api constructor( private http: HttpClient, private messageService: MessageService) { } /** GET heroes from the server */ getHeroes (): Observable<Hero[]> { return this.http.get<Hero[]>(this.heroesUrl) .pipe( tap(_ => this.log('fetched heroes')), catchError(this.handleError<Hero[]>('getHeroes', [])) ); } /** GET hero by id. Return `undefined` when id not found */ getHeroNo404<Data>(id: number): Observable<Hero> { const url = `${this.heroesUrl}/?id=${id}`; return this.http.get<Hero[]>(url) .pipe( map(heroes => heroes[0]), // returns a {0|1} element array tap(h => { const outcome = h ? `fetched` : `did not find`; this.log(`${outcome} hero id=${id}`); }), catchError(this.handleError<Hero>(`getHero id=${id}`)) ); } /** GET hero by id. Will 404 if id not found */ getHero(id: number): Observable<Hero> { const url = `${this.heroesUrl}/${id}`; return this.http.get<Hero>(url).pipe( tap(_ => this.log(`fetched hero id=${id}`)), catchError(this.handleError<Hero>(`getHero id=${id}`)) ); } /* GET heroes whose name contains search term */ searchHeroes(term: string): Observable<Hero[]> { if (!term.trim()) { // if not search term, return empty hero array. return of([]); } return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe( tap(_ => this.log(`found heroes matching "${term}"`)), catchError(this.handleError<Hero[]>('searchHeroes', [])) ); } //////// Save methods ////////// /** POST: add a new hero to the server */ addHero (hero: Hero): Observable<Hero> { return this.http.post<Hero>(this.heroesUrl, hero, httpOptions).pipe( tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)), catchError(this.handleError<Hero>('addHero')) ); } /** DELETE: delete the hero from the server */ deleteHero (hero: Hero | number): Observable<Hero> { const id = typeof hero === 'number' ? hero : hero.id; const url = `${this.heroesUrl}/${id}`; return this.http.delete<Hero>(url, httpOptions).pipe( tap(_ => this.log(`deleted hero id=${id}`)), catchError(this.handleError<Hero>('deleteHero')) ); } /** PUT: update the hero on the server */ updateHero (hero: Hero): Observable<any> { return this.http.put(this.heroesUrl, hero, httpOptions).pipe( tap(_ => this.log(`updated hero id=${hero.id}`)), catchError(this.handleError<any>('updateHero')) ); } /** * Handle Http operation that failed. * Let the app continue. * @param operation - name of the operation that failed * @param result - optional value to return as the observable result */ private handleError<T> (operation = 'operation', result?: T) { return (error: any): Observable<T> => { // TODO: send the error to remote logging infrastructure console.error(error); // log to console instead // TODO: better job of transforming error for user consumption this.log(`${operation} failed: ${error.message}`); // Let the app keep running by returning an empty result. return of(result as T); }; } /** Log a HeroService message with the MessageService */ private log(message: string) { this.messageService.add(`HeroService: ${message}`); } }import { InMemoryDbService } from 'angular-in-memory-web-api'; import { Hero } from './hero'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class InMemoryDataService implements InMemoryDbService { createDb() { const heroes = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombasto' }, { id: 14, name: 'Celeritas' }, { id: 15, name: 'Magneta' }, { id: 16, name: 'RubberMan' }, { id: 17, name: 'Dynama' }, { id: 18, name: 'Dr IQ' }, { id: 19, name: 'Magma' }, { id: 20, name: 'Tornado' } ]; return {heroes}; } // Overrides the genId method to ensure that a hero always has an id. // If the heroes array is empty, // the method below returns the initial number (11). // if the heroes array is not empty, the method below returns the highest // hero id + 1. genId(heroes: Hero[]): number { return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11; } }import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { HttpClientModule } from '@angular/common/http'; import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; import { InMemoryDataService } from './in-memory-data.service'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { DashboardComponent } from './dashboard/dashboard.component'; import { HeroDetailComponent } from './hero-detail/hero-detail.component'; import { HeroesComponent } from './heroes/heroes.component'; import { HeroSearchComponent } from './hero-search/hero-search.component'; import { MessagesComponent } from './messages/messages.component'; @NgModule({ imports: [ BrowserModule, FormsModule, AppRoutingModule, HttpClientModule, // The HttpClientInMemoryWebApiModule module intercepts HTTP requests // and returns simulated server responses. // Remove it when a real server is ready to receive requests. HttpClientInMemoryWebApiModule.forRoot( InMemoryDataService, { dataEncapsulation: false } ) ], declarations: [ AppComponent, DashboardComponent, HeroesComponent, HeroDetailComponent, MessagesComponent, HeroSearchComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
      
      
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3.  
  4. import { Observable, of } from 'rxjs';
  5. import { catchError, map, tap } from 'rxjs/operators';
  6.  
  7. import { Hero } from './hero';
  8. import { MessageService } from './message.service';
  9.  
  10. const httpOptions = {
  11. headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  12. };
  13.  
  14. @Injectable({ providedIn: 'root' })
  15. export class HeroService {
  16.  
  17. private heroesUrl = 'api/heroes'; // URL to web api
  18.  
  19. constructor(
  20. private http: HttpClient,
  21. private messageService: MessageService) { }
  22.  
  23. /** GET heroes from the server */
  24. getHeroes (): Observable<Hero[]> {
  25. return this.http.get<Hero[]>(this.heroesUrl)
  26. .pipe(
  27. tap(_ => this.log('fetched heroes')),
  28. catchError(this.handleError<Hero[]>('getHeroes', []))
  29. );
  30. }
  31.  
  32. /** GET hero by id. Return `undefined` when id not found */
  33. getHeroNo404<Data>(id: number): Observable<Hero> {
  34. const url = `${this.heroesUrl}/?id=${id}`;
  35. return this.http.get<Hero[]>(url)
  36. .pipe(
  37. map(heroes => heroes[0]), // returns a {0|1} element array
  38. tap(h => {
  39. const outcome = h ? `fetched` : `did not find`;
  40. this.log(`${outcome} hero id=${id}`);
  41. }),
  42. catchError(this.handleError<Hero>(`getHero id=${id}`))
  43. );
  44. }
  45.  
  46. /** GET hero by id. Will 404 if id not found */
  47. getHero(id: number): Observable<Hero> {
  48. const url = `${this.heroesUrl}/${id}`;
  49. return this.http.get<Hero>(url).pipe(
  50. tap(_ => this.log(`fetched hero id=${id}`)),
  51. catchError(this.handleError<Hero>(`getHero id=${id}`))
  52. );
  53. }
  54.  
  55. /* GET heroes whose name contains search term */
  56. searchHeroes(term: string): Observable<Hero[]> {
  57. if (!term.trim()) {
  58. // if not search term, return empty hero array.
  59. return of([]);
  60. }
  61. return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe(
  62. tap(_ => this.log(`found heroes matching "${term}"`)),
  63. catchError(this.handleError<Hero[]>('searchHeroes', []))
  64. );
  65. }
  66.  
  67. //////// Save methods //////////
  68.  
  69. /** POST: add a new hero to the server */
  70. addHero (hero: Hero): Observable<Hero> {
  71. return this.http.post<Hero>(this.heroesUrl, hero, httpOptions).pipe(
  72. tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),
  73. catchError(this.handleError<Hero>('addHero'))
  74. );
  75. }
  76.  
  77. /** DELETE: delete the hero from the server */
  78. deleteHero (hero: Hero | number): Observable<Hero> {
  79. const id = typeof hero === 'number' ? hero : hero.id;
  80. const url = `${this.heroesUrl}/${id}`;
  81.  
  82. return this.http.delete<Hero>(url, httpOptions).pipe(
  83. tap(_ => this.log(`deleted hero id=${id}`)),
  84. catchError(this.handleError<Hero>('deleteHero'))
  85. );
  86. }
  87.  
  88. /** PUT: update the hero on the server */
  89. updateHero (hero: Hero): Observable<any> {
  90. return this.http.put(this.heroesUrl, hero, httpOptions).pipe(
  91. tap(_ => this.log(`updated hero id=${hero.id}`)),
  92. catchError(this.handleError<any>('updateHero'))
  93. );
  94. }
  95.  
  96. /**
  97. * Handle Http operation that failed.
  98. * Let the app continue.
  99. * @param operation - name of the operation that failed
  100. * @param result - optional value to return as the observable result
  101. */
  102. private handleError<T> (operation = 'operation', result?: T) {
  103. return (error: any): Observable<T> => {
  104.  
  105. // TODO: send the error to remote logging infrastructure
  106. console.error(error); // log to console instead
  107.  
  108. // TODO: better job of transforming error for user consumption
  109. this.log(`${operation} failed: ${error.message}`);
  110.  
  111. // Let the app keep running by returning an empty result.
  112. return of(result as T);
  113. };
  114. }
  115.  
  116. /** Log a HeroService message with the MessageService */
  117. private log(message: string) {
  118. this.messageService.add(`HeroService: ${message}`);
  119. }
  120. }

HeroesComponent

<h2>My Heroes</h2> <div> <label>Hero name: <input #heroName /> </label> <!-- (click) passes input value to add() and then clears the input --> <button (click)="add(heroName.value); heroName.value=''"> add </button> </div> <ul class="heroes"> <li *ngFor="let hero of heroes"> <a routerLink="/detail/{{hero.id}}"> <span class="badge">{{hero.id}}</span> {{hero.name}} </a> <button class="delete" title="delete hero" (click)="delete(hero)">x</button> </li> </ul>import { Component, OnInit } from '@angular/core'; import { Hero } from '../hero'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-heroes', templateUrl: './heroes.component.html', styleUrls: ['./heroes.component.css'] }) export class HeroesComponent implements OnInit { heroes: Hero[]; constructor(private heroService: HeroService) { } ngOnInit() { this.getHeroes(); } getHeroes(): void { this.heroService.getHeroes() .subscribe(heroes => this.heroes = heroes); } add(name: string): void { name = name.trim(); if (!name) { return; } this.heroService.addHero({ name } as Hero) .subscribe(hero => { this.heroes.push(hero); }); } delete(hero: Hero): void { this.heroes = this.heroes.filter(h => h !== hero); this.heroService.deleteHero(hero).subscribe(); } }/* HeroesComponent's private CSS styles */ .heroes { margin: 0 0 2em 0; list-style-type: none; padding: 0; width: 15em; } .heroes li { position: relative; cursor: pointer; background-color: #EEE; margin: .5em; padding: .3em 0; height: 1.6em; border-radius: 4px; } .heroes li:hover { color: #607D8B; background-color: #DDD; left: .1em; } .heroes a { color: #888; text-decoration: none; position: relative; display: block; width: 250px; } .heroes a:hover { color:#607D8B; } .heroes .badge { display: inline-block; font-size: small; color: white; padding: 0.8em 0.7em 0 0.7em; background-color: #607D8B; line-height: 1em; position: relative; left: -1px; top: -4px; height: 1.8em; min-width: 16px; text-align: right; margin-right: .8em; border-radius: 4px 0 0 4px; } button { background-color: #eee; border: none; padding: 5px 10px; border-radius: 4px; cursor: pointer; cursor: hand; font-family: Arial; } button:hover { background-color: #cfd8dc; } button.delete { position: relative; left: 194px; top: -32px; background-color: gray !important; color: white; }
      
      
  1. <h2>My Heroes</h2>
  2.  
  3. <div>
  4. <label>Hero name:
  5. <input #heroName />
  6. </label>
  7. <!-- (click) passes input value to add() and then clears the input -->
  8. <button (click)="add(heroName.value); heroName.value=''">
  9. add
  10. </button>
  11. </div>
  12.  
  13. <ul class="heroes">
  14. <li *ngFor="let hero of heroes">
  15. <a routerLink="/detail/{{hero.id}}">
  16. <span class="badge">{{hero.id}}</span> {{hero.name}}
  17. </a>
  18. <button class="delete" title="delete hero"
  19. (click)="delete(hero)">x</button>
  20. </li>
  21. </ul>

HeroDetailComponent

<div *ngIf="hero"> <h2>{{hero.name | uppercase}} Details</h2> <div><span>id: </span>{{hero.id}}</div> <div> <label>name: <input [(ngModel)]="hero.name" placeholder="name"/> </label> </div> <button (click)="goBack()">go back</button> <button (click)="save()">save</button> </div>import { Component, OnInit, Input } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Location } from '@angular/common'; import { Hero } from '../hero'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-hero-detail', templateUrl: './hero-detail.component.html', styleUrls: [ './hero-detail.component.css' ] }) export class HeroDetailComponent implements OnInit { @Input() hero: Hero; constructor( private route: ActivatedRoute, private heroService: HeroService, private location: Location ) {} ngOnInit(): void { this.getHero(); } getHero(): void { const id = +this.route.snapshot.paramMap.get('id'); this.heroService.getHero(id) .subscribe(hero => this.hero = hero); } goBack(): void { this.location.back(); } save(): void { this.heroService.updateHero(this.hero) .subscribe(() => this.goBack()); } }
      
      <div *ngIf="hero">
  <h2>{{hero.name | uppercase}} Details</h2>
  <div><span>id: </span>{{hero.id}}</div>
  <div>
    <label>name:
      <input [(ngModel)]="hero.name" placeholder="name"/>
    </label>
  </div>
  <button (click)="goBack()">go back</button>
  <button (click)="save()">save</button>
</div>
    

DashboardComponent

<h3>Top Heroes</h3> <div class="grid grid-pad"> <a *ngFor="let hero of heroes" class="col-1-4" routerLink="/detail/{{hero.id}}"> <div class="module hero"> <h4>{{hero.name}}</h4> </div> </a> </div> <app-hero-search></app-hero-search>
      
      <h3>Top Heroes</h3>
<div class="grid grid-pad">
  <a *ngFor="let hero of heroes" class="col-1-4"
      routerLink="/detail/{{hero.id}}">
    <div class="module hero">
      <h4>{{hero.name}}</h4>
    </div>
  </a>
</div>

<app-hero-search></app-hero-search>
    

HeroSearchComponent

<div id="search-component"> <h4>Hero Search</h4> <input #searchBox id="search-box" (input)="search(searchBox.value)" /> <ul class="search-result"> <li *ngFor="let hero of heroes$ | async" > <a routerLink="/detail/{{hero.id}}"> {{hero.name}} </a> </li> </ul> </div>import { Component, OnInit } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Hero } from '../hero'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-hero-search', templateUrl: './hero-search.component.html', styleUrls: [ './hero-search.component.css' ] }) export class HeroSearchComponent implements OnInit { heroes$: Observable<Hero[]>; private searchTerms = new Subject<string>(); constructor(private heroService: HeroService) {} // Push a search term into the observable stream. search(term: string): void { this.searchTerms.next(term); } ngOnInit(): void { this.heroes$ = this.searchTerms.pipe( // wait 300ms after each keystroke before considering the term debounceTime(300), // ignore new term if same as previous term distinctUntilChanged(), // switch to new search observable each time the term changes switchMap((term: string) => this.heroService.searchHeroes(term)), ); } }/* HeroSearch private styles */ .search-result li { border-bottom: 1px solid gray; border-left: 1px solid gray; border-right: 1px solid gray; width: 195px; height: 16px; padding: 5px; background-color: white; cursor: pointer; list-style-type: none; } .search-result li:hover { background-color: #607D8B; } .search-result li a { color: #888; display: block; text-decoration: none; } .search-result li a:hover { color: white; } .search-result li a:active { color: white; } #search-box { width: 200px; height: 20px; } ul.search-result { margin-top: 0; padding-left: 0; }
      
      
  1. <div id="search-component">
  2. <h4>Hero Search</h4>
  3.  
  4. <input #searchBox id="search-box" (input)="search(searchBox.value)" />
  5.  
  6. <ul class="search-result">
  7. <li *ngFor="let hero of heroes$ | async" >
  8. <a routerLink="/detail/{{hero.id}}">
  9. {{hero.name}}
  10. </a>
  11. </li>
  12. </ul>
  13. </div>

小结

Summary

旅程即将结束,不过你已经收获颇丰。

You're at the end of your journey, and you've accomplished a lot.

  • 你添加了在应用程序中使用 HTTP 的必备依赖。

    You added the necessary dependencies to use HTTP in the app.

  • 你重构了 HeroService,以通过 web API 来加载英雄数据。

    You refactored HeroService to load heroes from a web API.

  • 你扩展了 HeroService 来支持 post()put()delete() 方法。

    You extended HeroService to support post(), put(), and delete() methods.

  • 你修改了组件,以允许用户添加、编辑和删除英雄。

    You updated the components to allow adding, editing, and deleting of heroes.

  • 你配置了一个内存 Web API。

    You configured an in-memory web API.

  • 你学会了如何使用“可观察对象”。

    You learned how to use observables.

《英雄指南》教程结束了。 如果你准备开始学习 Angular 开发的原理,请开始 架构 一章。

This concludes the "Tour of Heroes" tutorial. You're ready to learn more about Angular development in the fundamentals section, starting with the Architecture guide.