简体   繁体   English

如何在Angular2中管理服务?

[英]How to manage Services in Angular2?

Angular 2 : 2.0.0-alpha.31 / Typescript 1.5 Angular 2 :2.0.0-alpha.31 / Typescript 1.5

Currently I manage my service as a simple Class , then I inject this Class into an other component. 目前我将我的服务作为一个简单的Class ,然后我将这个Class注入另一个组件。 Example: 例:

export class PlayerService {
  http: Http;
  players = [];

  constructor( @Inject(Http) http) {
        this.http = http;
  }

  getAll(callback) {
        this.http.get('/data/players.json')
              .toRx()
              .map((res) => res.json())
              .subscribe((data) => {
                    this.players= data;
                    callback(data);
              });
  }

  add(player) {
        //call webservice to save user  
        this.players.push(player); //only if save has work  
  }
  delete(player) {
        //Not implemented yet   
  }
  get(id) {
        //Not implemented yet   
  }
}

I think, I'm doing it the wrong way.. 我想,我这样做的方式不对..

  • I'm using http.get().toRx().subscribe() ? 我正在使用http.get().toRx().subscribe() I thought I saw that some people return the Observable directly from toRx() 我以为我看到有人将Observable直接从toRx()
  • If two components ask for players ( getAll() ) at the same time, two queries will be executed. 如果两个组件同时请求玩家( getAll() ),则将执行两个查询。 Do I have to manage flag or is there another way ? 我必须管理旗帜还是有另一种方式?
  • I'm working here with a callback. 我在这里工作回调。 What do I have to do if I want the data "immediately" (no async) 如果我想“立即”(没有异步)数据我该怎么办?
  • Will components be automatically informed about players add/remove ? components会自动通知玩家添加/删除吗? Do I have to use some kind of event to handle this (any example?) ? 我是否必须使用某种事件来处理这个问题(任何一个例子?)?

So my question is : 所以我的问题是:

  • Is there a common way to manage Services in Angular2 ? 是否有一种在Angular2中管理服务的常用方法?

First of all the service is well done as you did it, and it's the way to go with Angular2: A Class injected into the other components. 首先,服务很好,就像你做的那样,这是使用Angular2的方法:将一个Class注入到其他组件中。

That said, about the other issues you raise, I'd rather return and store the result in a promise instead of using a callback: 也就是说,关于你提出的其他问题,我宁愿返回并将结果存储在promise中,而不是使用回调:

getAll() {
    return players || players = new Promise(
        (resolve) => this.http.get('people.json').observer({next: resolve})
    );
}

Note : You should be able to use Observable.toPromise() but for some reason it's not working for me in Angular2 注意 :您应该可以使用Observable.toPromise()但由于某种原因它在Angular2中不适用于我

This way further calls to getAll() will not trigger more responses. 这样进一步调用getAll()不会触发更多响应。

To the synchronous questions, you should not do that. 对于同步问题,你不应该这样做。 Now that you have it inside a promise, just call getAll() and return a promise when ever a player is requested. 现在你已经将它放在一个承诺中,只需调用getAll()并在请求玩家时返回一个承诺。

get(id) {
    return getAll().then((result) => {return Promise.resolve(result[id])});  
}

About the way to handle Players additions and removals, that's exactly what Observables are meant to in RxJS. 关于处理玩家添加和删除的方式,这正是Observables在RxJS中的意图。 You need to provide and Observable stream that notifies it's observers when the player list changes. 您需要提供和Observable流,以便在播放器列表更改时通知其观察者。 You can use the EventEmitter for that: 你可以使用EventEmitter

constructor( @Inject(Http) http) {
    this.http = http;
    this.myEventEmitter = new EventEmitter();
}

getUpdateStream() {
    myEventEmitter.toRx();
}

When you change the Players list just do myEventEmitter.next(players) 当您更改玩家列表时,只需执行myEventEmitter.next(players)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM