我有兩個應通過Observable()連接的組件。可觀測不觸發
在第一個我聲明
@Injectable()
export class One{
private changeConfirmed = new Subject<boolean>();
changeConfirmed$ = this.changeConfirmed.asObservable();
public init$: EventEmitter<boolean>;
registerChangeConfirmed(category:boolean){
alert('sending')
this.changeConfirmed.next(category);
}
onMsg(){
this.registerChangeConfirmed(true);
}
}
onMsg
的事件勢必模板
,並然而,事件沒有得到發出的第二個
Import { One } from './pat/to/it'
@Component({
providers:[One]
})
export class two{
constructor(private childClass : One){
this.childClass.changeConfirmed$.subscribe(x => {alert(x)})
}
}
。 但是,當我發出兩班,而不是一類事件= I包括
this.childClass.registerChangeConfirmed(true)
兩個類的事件被觸發。當我從第一堂課調用它時,爲什麼它不工作?
不應在服務中使用'EventEmitter'。僅適用於組件和指令中的@Output()。另請參閱http://stackoverflow.com/a/34402436/217408 –
「一」是組件還是服務?謝謝! –
@ThierryTemplier實際上,一個是 – Johnyb