angular2组件中的火灾事件
我有两个部分:angular2组件中的火灾事件,angular,Angular,我有两个部分: app.component site.component 我想从应用程序组件中的站点组件捕获事件。 但从路由器渲染的站点组件(当我单击某个链接时) 现在在站点组件中,我有: @Component({ selector: 'app-sites-list', templateUrl: './sites-list.component.html', styleUrls: ['./sites-list.component.css'], outputs: ['counter
app.component
site.component
我想从应用程序组件中的站点组件捕获事件。
但从路由器渲染的站点组件(当我单击某个链接时)
现在在站点组件中,我有:
@Component({
selector: 'app-sites-list',
templateUrl: './sites-list.component.html',
styleUrls: ['./sites-list.component.css'],
outputs: ['counterChange'],
})
export class Sites implements OnInit {
public counterChange = new EventEmitter();
在tmplate中:
<button type="submit" (click)="check(url.value);">Go</button>
Go
那么,如何在应用程序组件中捕获此事件。感谢不在父/子层次结构中的组件通常通过可注入服务进行通信。类似这样的内容(我没有测试此代码): 在组件1中,发送方:
constructor(myService:MyService) {
}
check(value) {
this.myService.notify(value);
}
在组件2中,接收器:
constructor(myService:MyService) {
myService.notifierSubject.subscribe(data => { console.log(data); });
}
在这个例子中,我从RxJS中选择了一个主题。当然,你可以自由选择你想要的任何东西。你可以订阅的所有东西都应该能完成这项工作
constructor(myService:MyService) {
myService.notifierSubject.subscribe(data => { console.log(data); });
}