简体   繁体   English

父类的Angular2 EventEmitter

[英]Angular2 EventEmitter from parent class

I created a abstract super class to handle generic factors for various form components within my app. 我创建了一个抽象超类来处理我的应用程序中各种表单组件的泛型因素。

For some reason I cannot properly catch events emitted by a derived class who's EventEmitter is declared in a superclass. 出于某种原因,我无法正确捕获派生类发出的事件,其中EventEmitter是在超类中声明的。

Superclass: 超类:

export abstract class Form<T> implements OnInit {
    @Output() submitted : EventEmitter<T> = new EventEmitter<T>();
    fb                  : FormBuilder     = new FormBuilder();
    formModel           : ControlGroup;

    abstract ngOnInit() : any;
}

Derived: 衍生:

export class LoginFormComponent extends Form<Login> {

    credentials : Login = new Login();

    constructor() {
        super();
    }

    doLogin() {
        this.submitted.emit(this.credentials);
    }
}

HTML of encapsulating element: 封装元素的HTML:

<login-form (submitted)="login($event)"></login-form>\

When I move the EventEmitter to the derived class, everything works like a charm. 当我将EventEmitter移动到派生类时,一切都像魅力一样。 What do I need to change to allow this to work? 我需要更改什么才能使其正常工作?

Such decorators in super-classes are not recognized by Angular. Angular不承认超类中的这种装饰器。

You can enable them using the @Component() annotation 您可以使用@Component()注释启用它们

@Component({
  selector: '...',
  outputs: ['submitted'],
  inputs: ['...']
})

See also https://github.com/angular/angular/issues/5415 另见https://github.com/angular/angular/issues/5415

The problem is that the @Component decorator doesn't support inheritance (elements defined in the parent class). 问题是@Component装饰器不支持继承(父类中定义的元素)。 It takes into account only elements defined in the current decorated class. 它仅考虑当前修饰类中定义的元素。

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

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