简体   繁体   English

从组件到app.component的角度EventEmitter

[英]Angular EventEmitter from component to app.component

I would like to bind an event emitter from a child to a parent component, but the parent component is the 'app.component'so i assume that the relationship between the two is not declared but implicit. 我想将事件发射器从子级绑定到父级组件,但是父级组件是'app.component',因此我假设两者之间的关系未声明而是隐式的。
in this answer , i see how to add an event emitter to a child that has been explicitly declared as such, in his parent: 这个答案中,我看到了如何向其父中已显式声明的孩子添加事件发射器:

<child (notifyParent)="getNotification($event)"></child>

But in my app, the child component (lets call id: child), is not declared in that way, but its declared in the app.module as route: 但是在我的应用中,子组件( app.module称为id:child)不是以这种方式声明的,而是在app.module声明为route的:

const appRoutes: Routes = [
  { path: '**',      component:  Child}
];

This component basically represents a splash page to the site, and is also used as a redirect in a guard in case full authentication is not provided. 该组件基本上代表网站的初始页面,并且在没有提供完整身份验证的情况下,也用作防护中的重定向。
What i would like to do, is to add an event emitter to child that notifies app.module of a change. 我想这样做,是增加一个事件发射到child通知app.module的变化。
I have already implemented, in child an EventEmitter with the @Output() decorator, and i have also implemented the method that will be invoked in app.module but i just cant find a place where to register this binding within the two components. 我已经用@Output()装饰器在child实现了一个EventEmitter ,并且我还实现了将在app.module调用的方法,但我只是找不到在两个组件中注册此绑定的地方。
Since all components are children of app.component should not app.component provide a way to register for incoming input from its children, without the need of declaring them explicitly as children first? 由于所有部件都是孩子app.component不应app.component提供一种方式来从它的孩子进入输入寄存器,而不需要明确声明他们作为孩子第一的?

Well, from my understanding you'll have to explicitly bind the app.component and child.component to actually make the Event emitter work. 好吧,据我所知,您必须显式绑定app.componentchild.component才能真正使事件发射器正常工作。

From your scenario, you can also consider using Subject to achieve the same. 从您的方案中,您还可以考虑使用Subject来实现相同目的。 You dont have to explicitly define for every child.component , rather you can simply Subscribe and listen to any changes that are happening. 您不必为每个child.component显式定义,而是只需订阅并收听正在发生的任何更改。

I have created one link to help two sibling & parent components talk . 我创建了一个链接来帮助两个同级和父级组件进行对话 You can do the same for Several child and one parent 您可以对几个孩子和一个父母做同样的事情

A parent-child component relation is given when your 'child' component is called in your 'parents' html template. 当在“父母” html模板中调用“子”组件时,将给出父子组件关系。 So if your component is in your app.components html template then it's a child of app.component and you can pass data between the two using EventEmitters . 因此,如果您的组件位于app.components html模板中,则它是app.component的子级,您可以使用EventEmitters在两者之间传递数据。

If there is another component between those two, then you'd have to use a service to share data. 如果这两个组件之间还有其他组件,则必须使用服务来共享数据。

You will have to add 您将必须添加

<child (notifyParent)="getNotification($event)"></child>

In your parent component template ie app.component in your case, in order to listen to the "notifyParent" event. 在您的父组件模板中,即您的情况下为app.component,以便侦听“ notifyParent”事件。 Move the method implementation from app.module into app.component.ts file and it will work. 将方法实现从app.module移到app.component.ts文件中,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM