我正在使用angular,试图与非父子组件进行通信。所以我通过服务来传达它
service.ts
Istoggle=false;
@Output() change: EventEmitter < boolean > = new EventEmitter();
toggle() {
this.Istoggle= !this.Istoggle;
this.toggle.emit(this.Istoggle);
}search.ts
submit():void{
this.service.toggle();
}Home.ts
ngOnInit() {
this.service.change.subscribe(_toggle=> {
//some code here
}
}因此,当我在Home component中单击submit时,切换subscribe时会点击两次
发布于 2019-02-20 21:39:40
正如我所看到的,您的服务中有3个名称完全相同的字段。其中两个被遗漏了。你可以这样做
value=false;
search: EventEmitter < boolean > = new EventEmitter();
toggle() {
this.value = !this.value;
this.search.emit(this.value);
}在您的服务和组件中:
ngOnInit() {
this.service.search.subscribe(value => {
//some code here
}
}请注意,我删除了@Output()装饰器。它仅在组件内部使用,仅用于父子通信。
https://stackoverflow.com/questions/54784980
复制相似问题