【问题标题】:Angular 2 Official Tutorial - Promise.resolve VS httpAngular 2 官方教程 - Promise.resolve VS http
【发布时间】:2016-06-24 03:14:38
【问题描述】:

在 Angular 2 的官方教程中,他们使用 Promise.resolve(HEROES) 来获取 HEROES 数组。

我尝试按照 Http Client 教程进行操作,但遇到了一些问题。我可以发出请求,但是当我导航到其他页面时,它会再次发出 get 请求。不同的组件使用他们自己的 HEROES。

我想知道他们为什么会有不同的行为。如果我想与服务器通信并且仍然使组件使用同一组 HEROES,我该怎么办?

教程现场示例:(https://angular.io/resources/live-examples/toh-5/ts/plnkr.html)

我的试用:(http://plnkr.co/edit/mPgpt2snK2OY6o8sq6YK?p=preview)

我的不同之处在于……

(1) Angular 官方版本的app/hero.service.ts

import { Hero } from './hero';
import { HEROES } from './mock-heroes';
import { Injectable } from 'angular2/core';

@Injectable()
export class HeroService {
  getHeroes() {
    return Promise.resolve(HEROES);
  }

  getHero(id: number) {
    return Promise.resolve(HEROES).then(
      heroes => heroes.filter(hero => hero.id === id)[0]
    );
  }
}

(2) 我的app/hero.service.ts

版本
import {Http, Response} from 'angular2/http';
import {Headers, RequestOptions} from 'angular2/http';
import {Observable}     from 'rxjs/Observable';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
import { Injectable } from 'angular2/core';

@Injectable()
export class HeroService {
  constructor (private http: Http) {}
  getHeroes() {
    return this.http.get("hero.json")
                    .map(res => <Hero[]> res.json().HEROES)
                    .do(data => console.log(data)) // eyeball results in the console
                    .catch(this.handleError);
  }

    getHero(id: number) {
    return this.http.get("hero.json")
                    .map(res => <Hero> res.json().data.filter(hero => hero.id === id)[0])
                    .do(data => console.log(data)) // eyeball results in the console
                    .catch(this.handleError);
  }
  private handleError (error: any) {
    // in a real world app, we may send the error to some remote logging infrastructure
    // instead of just logging it to the console
    console.error(error);
    return Promise.reject(error.message || error.json().error || 'Server error');
  }

}

【问题讨论】:

    标签: angular


    【解决方案1】:

    我猜你想要类似的东西:

      getHeroes() {
        if(this.data) {
          return Observable.of(this.data);
        } else {
        return this.http.get("hero.json")
                        .map(res => <Hero[]> res.json().HEROES)
                        .do(data => {
                           console.log(data)) // eyeball results in the console
                           this.data = data;
                         });
                        .catch(this.handleError);
        };
      }
    

    【讨论】:

    • 好的,谢谢!我已经更新了链接并且它有效。你能解释更多关于 Observable 的概念吗? (或者发送入门指南很好)再次感谢!
    • PromiseObservable 提供了一个统一的 API 来处理异步执行(以避免回调地狱)。您可以将回调传递给Promise.then(...),当异步调用(例如对服务器的请求)完成时调用该回调。如果您有一系列事件,希望为每个事件调用回调,Observable 是更好的选择。与then(...).then(...)Promise 一样使用then(...).then(...) 链接后续异步调用,而是可以与Observable 一起使用的各种运算符的长列表。
    • 参见github.com/ReactiveX/RxJS/blob/master/MIGRATION.md(Angular2 使用 RxJS 5)或youtube.com/watch?v=UHI0AzD_WfY 了解更多详情。那里有很多文档。 Angular 只使用 RxJS 并且可以很好地使用它。虽然 RxJS 不是 Angular 的一部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2018-11-29
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多