2016-11-25 6 views
1

私は、angle2アプリケーションのコンポーネントの通信サービスとして件名のサービスを使用しています。2つのパラメータでsubject(rx.js)を作成する方法は?

呼び出す方法:

this.notification.create('test'); 

サービス:

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

    notiCreated$ = NotificationService.notificationSource.asObservable(); 

    create(message) { 
    NotificationService.notificationSource.next(message); 
    } 
} 

と呼ばれる関数:

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

しかし、私は2つのparamsを通過したいです。そして私には誤りがある。

どうすればいいですか?

答えて

5

nextのAPIはnext(value: T): voidであるため、1つのパラメータしか取ることができません。 参考:回避策としてhttp://reactivex.io/rxjs/class/es6/Subscriber.js~Subscriber.html

することができますグループオブジェクト内のメッセージ:

this.notification.create({message1:'test', message2:'test2'}); 

、あなたが消費したときに、ちょうどあなたが必要とするメッセージを選択:

this.subscription = notification.notiCreated$.subscribe(
    data => { 
     console.log(data.message1, data.message2); 
     this.createNotification(data.message1, 'warning'); 
    }); 
+0

を行うことは、それは不可能ですがそれは2つのパラメータで? –

+0

@MaxKarpovetsいいえ、 'next'のAPIは' next(value:T):void; 'であるため、1つのパラメータしか必要としません。 – echonax

+1

本当に助けてくれます。ありがとう –

関連する問題