Rxjs封装基础EventBus(基于Angular)

import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';

// 通用组件通信类
@Injectable({
providedIn: 'root',
})

export class EventBusService {

private eventSubject: Subject = new Subject();
private ob$ = this.eventSubject.asObservable();

public Observable(type: EventType): Observable {
    return this.ob$.pipe(filter(e => e.type === type));
}

send(e: EBEvent) {
    this.eventSubject.next(e);
}

constructor() { }

}

export class EBEvent {
constructor(public type: EventType, public data?: any) { }
}

export enum EventType {
// 知识库发送详细内容到聊天窗口
SEND_KNOWLEDGE_DETAIL_TO_WINDOW = 'SEND_KNOWLEDGE_DETAIL_TO_WINDOW'
}

你可能感兴趣的:(Rxjs封装基础EventBus(基于Angular))