创建和角2服务返回观测 [英] Creating and returning Observable from Angular 2 Service

查看:124
本文介绍了创建和角2服务返回观测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多的是一种最佳做法的问题。有三名球员:一个组件,一个服务模式。在组件呼吁的服务从数据库中获取数据。在服务使用:

This is more of a "best practices" question. There are three players: a Component, a Service and a Model. The Component is calling the Service to get data from a database. The Service is using:

this.people = http.get('api/people.json').map(res => res.json());

返回一个观测

组件可以只订阅观测

    peopleService.people
        .subscribe(people => this.people = people);
      }

不过,我真正想要的是服务返回一个这是从创建模式的阵列对象从数据库中检索到服务中的数据。我意识到组件可能只是创建这个数组中的订阅方法,但我认为这将是清洁的,如果服务做,并使其可在组件

However, what I really want is for the Service to return an Array of Model objects that was created from the data that the Service retrieved from the database. I realized that the Component could just create this array in the subscribe method, but I think it would be cleaner if the service do that and make it available to the Component.

如何能在服务创建一个新的观测,包含数组,并返回?

How can the Service create a new Observable, containing that array, and return that?

推荐答案

我觉得这是一个使用的<一个用例href=\"https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/subjects/subject.md\">Observable主题或在 Angular2 EventEmitter

I think this is a use case of using a Observable Subject or in Angular2 the EventEmitter.

在你的服务,你创建一个 EventEmitter ,允许你将值到它。在阿尔法45 您必须将其与转换梅花头(),但我知道他们正在努力摆脱这一点,所以阿尔法46 您可以简单地返回 EvenEmitter

In your service you create a EventEmitter that allows you to push values onto it. In Alpha 45 you have to convert it with toRx(), but I know they were working to get rid of that, so in Alpha 46 you may be able to simply return the EvenEmitter.

class EventService {
  _emitter: EventEmitter = new EventEmitter();
  rxEmitter: any;
  constructor() {
    this.rxEmitter = this._emitter.toRx();
  }
  doSomething(data){
    this.rxEmitter.next(data);
  }
}

这方式有单 EventEmitter 您不同的服务功能,现在可以推入。

This way has the single EventEmitter that your different service functions can now push onto.

如果您想直接从调用返回一个可观察的,你可以做这样的事情:

If you wanted to return an observable directly from a call you could do something like this:

myHttpCall(path) {
    return new Observable.create(observer => {
        http.get(path).map(res => res.json()).subscribe((result) => {
            //do something with result. 
            var newResultArray = mySpecialArrayFunction(result);
            observer.next(newResultArray);
            //call complete if you want to close this stream (like a promise)
            observer.complete();
        });
    });
}

这将允许你在组件中做到这一点:
peopleService.myHttpCall(路径)认购(人=&GT; this.people =人);

That would allow you do this in the component: peopleService.myHttpCall('path').subscribe(people => this.people = people);

和乱用来自服务调用的结果。

And mess with the results from the call in your service.

我喜欢创造的情况下对自己的 EventEmitter 流我需要从其他组件访问它,但我可以看到这两个方面的工作...

I like creating the EventEmitter stream on its own in case I need to get access to it from other components, but I could see both ways working...

下面是显示了事件发射器的基本服务plunker: Plunkr

Here's a plunker that shows a basic service with an event emitter: Plunkr

这篇关于创建和角2服务返回观测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆