【发布时间】:2018-08-05 14:10:00
【问题描述】:
以下 Angular 6 服务可用于向/从其他服务/组件发送/接收消息(注意 getMsg 返回一个 Observable):
@Injectable({
providedIn: 'root'
})
export class MessageService {
private subject = new Subject<any>();
sendMsg(message: any) {
this.subject.next(message);
}
getMsg(): Observable<any> {
return this.subject.asObservable();
}
}
现在,我需要允许发送方/接收方使用通道,类似于 $emit/broadcast 中的 AngularJS 事件名称。 sendMsg 签名如下:
sendMsg(channel: string, message: any)
为了接收消息,接收者必须使用频道名称订阅 Observable。这可能吗?
注意:我不想在订阅中使用filter,因为如果消息数量很大,它会影响性能。我更喜欢使用 Rxjs 原生选项。
【问题讨论】:
-
你不想使用
filter,因为你想要一个 RxJS 选项?我很困惑... -
原生选项 = 命名频道