简体   繁体   English

具有角度服务的EventEmitter或RxJS主题

[英]EventEmitter or RxJS Subject in angular service

  1. Is EventEmitter an RxJS Observable? EventEmitter是一个RxJS Observable吗?
  2. In the angular documentation, it explains how to communicate from child to parent component using EventEmitter. 在角度文档中,它解释了如何使用EventEmitter从子组件到父组件进行通信。 Should we use EventEmitter only in component or it can be used angular service also? 我们应该只在组件中使用EventEmitter还是可以使用角度服务?
  3. In the angular documentation, it explains how parent and children communicate via a shared service that uses observables RxJS Subject. 在角度文档中,它解释了父和子如何通过使用observables RxJS Subject的共享服务进行通信。 Can we use EventEmitter instead of RxJS Subject in this MissionService example? 我们可以在此MissionService示例中使用EventEmitter而不是RxJS Subject吗? Please help converting this example with EventEmitter if we can use EventEmitter at all in a service. 如果我们可以在服务中使用EventEmitter,请使用EventEmitter帮助转换此示例。 I'm new to angular. 我很有棱角。

    https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

I'm confused a bit after reading these related posts: 阅读这些相关帖子后我有点困惑:

EventEmiter extends from RxJs subject and you can use it as Observable. EventEmiter从RxJs主题扩展而来,您可以将它用作Observable。

Angular sources 角度来源

    export declare class EventEmitter<T> extends Subject<T> {
      __isAsync: boolean;
      constructor(isAsync?: boolean);
      emit(value?: T): void;
      subscribe(generatorOrNext?: any, error?: any, complete?: any): any;
    }

The best practice to sharing data between parent and child components use @Input and @Output 在父组件和子组件之间共享数据的最佳做法是使用@Input和@Output

When you need to use service for sharing. 当您需要使用服务进行共享时。 You need to use Subject or BehaviorSubject 您需要使用Subject或BehaviorSubject

service example 服务示例

@Injectable()
export class MyService {
  private data: BehaviorSubject<MyData> = new BehaviorSubject(null);

  getData(): Observable<MyData> {
    return this.data;
  }

  setData(d: MyData): void {
    this.data.next(d);
  }
}

use in component 在组件中使用

data: Observable<MyData>;

constructor(private myService: MyService) {}

ngOnInit() {
   this.data = this.myService.getData();
}

use in template 在模板中使用

<div>{{data|async}}</div>

There are many different ways to handle event scenario's. 有许多不同的方法来处理事件场景。

The EventEmitter is the most common way to communicate child events to the parent. EventEmitter是将子事件传递给父级的最常用方法。 Say you created a child component and clicked a button there, you might want the clicked event in the parent: 假设您创建了一个子组件并在那里单击了一个按钮,您可能希望在父组件中单击该事件:

<button (click)="clickEmitter.emit()">button</button>

<child-component (clickEmitter)="buttonClicked()"></child-component>

A shared service (Injectable) can be used to store data for multiple components. 共享服务(Injectable)可用于存储多个组件的数据。

A Subject and BehaviorSubject can be used to share events between components (sometimes via the shared service). Subject和BehaviorSubject可用于在组件之间共享事件(有时通过共享服务)。 For example: i used an authService with a user BehaviorSubject inside to get the logged in user object in every component. 例如:我使用了一个带有用户BehaviorSubject的authService来获取每个组件中的登录用户对象。

These are just some simple examples amongst many other use-cases. 这些只是许多其他用例中的一些简单示例。

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

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