简体   繁体   English

EventEmitter angular 2捕获事件和调试

[英]EventEmitter angular 2 catching event and debugging

I have write a simple event emitter but it seems not working, so that's the code: 我编写了一个简单的事件发射器,但它似乎不起作用,因此代码如下:

AppComponent AppComponent

and main app component 和主要的应用程序组件

@Component({
    selector:'my-app',
    templateUrl:'app/app.component.html',
    styleUrls:['app/app.component.css'],
    directives:[ROUTER_DIRECTIVES,MenuBar],
})
export class AppComponent{
    title:'My App'
    activated(event:Routes){
        console.log('activated');
        console.log(received)
    }
}

AppComponent.html AppComponent.html

<h1>{{title}}</h1>
<menu-bar (activate)="activated($event)"></menu-bar>
<div class="router-wrapper">
    <router-outlet></router-outlet>
</div>

Download 下载

@Component({
    .... component's configurations....
})
class Download implements OnInit{
    ... some attributes and code ...;

    @Output() activate:EventEmitter <Routes>= new EventEmitter<Routes>();
    r:Routes={};
    test(): void{
        console.log('emit start')
        console.log(this.r);
        try{
            this.activate.emit(this.r)
        }
        catch(err){
            console.log(err)
        }
        console.log('sent')
    }
}

the download component is inside a router-outlet. 下载组件位于路由器出口内。 So first of all, I see all the console.log messages of the Download component but I see no message in the app component is there any reason? 因此,首先,我看到了Download组件的所有console.log消息,但我看不到app组件中的消息是什么原因? What am I doing wrong? 我究竟做错了什么?
Second one, I think (but maybe I'm wrong) that the problem is in the message propagations, so I would like to debug the emit and catch the events globally, is it possible? 第二个问题,我认为(但也许我错了)问题出在消息传播中,所以我想调试发出并全局捕获事件,这可能吗? And if it is so, how shall I do that? 如果是这样,我该怎么做?

Events from @Outputs() don't bubble and are useless in routed components. @Outputs()事件不会冒泡,并且在路由组件中没有用。

You can only use it like <download-component (activate)="..." (listening on the component directly) 您只能像<download-component (activate)="..." (直接在组件上收听<download-component (activate)="..."那样使用它

You can use a shared service to communicate between routed components and parent components like explained in https://angular.io/docs/ts/latest/cookbook/component-communication.html 您可以使用共享服务在路由的组件和父组件之间进行通信,如https://angular.io/docs/ts/latest/cookbook/component-communication.html中所述

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

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