如何用2个参数创建主题(rx.js)?

Max*_*x K 7 subject rxjs typescript angular2-components angular

我正在使用subject作为我的angular2应用程序中组件的通信服务的服务.

通话方式:

this.notification.create('test');
Run Code Online (Sandbox Code Playgroud)

服务:

export class NotificationService {
  private static notificationSource = new Subject<any>()

  notiCreated$ = NotificationService.notificationSource.asObservable();

  create(message) {
    NotificationService.notificationSource.next(message);
  }
}
Run Code Online (Sandbox Code Playgroud)

被叫功能:

  this.subscription = notification.notiCreated$.subscribe(
    data => {
       console.log(data);
       this.createNotification(data, 'warning');
  });
Run Code Online (Sandbox Code Playgroud)

但我想通过2个参数.我有错误.

我该怎么做?

eko*_*eko 9

由于API的next就是next(value: T): void,就只能采取一个参数.参考:http://reactivex.io/rxjs/class/es6/Subscriber.js~Subscriber.html

作为一种解决方法,您可以将消息分组到对象中:

this.notification.create({message1:'test', message2:'test2'});
Run Code Online (Sandbox Code Playgroud)

当你消费时,只需选择你需要的信息:

 this.subscription = notification.notiCreated$.subscribe(
    data => {
       console.log(data.message1, data.message2);
       this.createNotification(data.message1, 'warning');
  });
Run Code Online (Sandbox Code Playgroud)

  • 你真的帮助我.谢谢 (2认同)