2016-11-25 4 views
1

내 angular2 응용 프로그램의 구성 요소에 대한 통신 서비스로 subject가있는 서비스를 사용하고 있습니다.2 개의 매개 변수로 주제 (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이므로 하나의 매개 변수 만 사용할 수 있습니다. 참조 : 해결 방법으로 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 param 함께? –

+0

@MaxKarpovets 'next'의 API가'next (value : T) : void; '이므로 1 개의 매개 변수 만 필요합니다. – echonax

+1

정말 도움이됩니다. 감사 –