2017-07-31 55 views
0

我想创建一个接口,以强制执行使用@Input()@Output()的任何人。确保执行组件输入和输出角度4

这是我的接口:

export interface BannerInterface { 
    currentEvents: Input, 
    _bannerClick: Output 
} 

而我的组件,它实现它:

@Input() 
    set currentEvents(currentEvents: any[]) { 
    this.currentEvent = currentEvents[0]; 
    } 

    @Output() _bannerClick: EventEmitter<any> = new EventEmitter(); 

这给了我,我错误地使用接口的错误。 我明白接口可以强制执行的类型,而不是装饰(我的构建不能使用这些类型的接口),但我想知道如果有,我可以强制性质是@Input()@Output()的方式。

+0

所以基本上你可以强制装饰?我不认为这是甚至是可能的,也不需要 –

+0

这是我想要实现的。强制每个实现此接口的组件都具有某些属性,这些属性是Input()和Output() –

+0

为什么你需要这个? –

回答

0

你可以使用抽象类的每个组件:

export abstract class Component { 
    @Input() 
    abstract set currentEvents(currentEvents: any[]); 

    @Output() 
    abstract get output(); 
} 

export class FooComponent extends Component{ 
    set currentEvents(currentEvents: any[]) { 
     //Your implementation. 
    } 

    get output() { 
     //Your implementation. 
    } 
} 

我没有工具,目前测试这个但这应该工作,因为装饰遵循继承。无论如何,我没有看到你想要这样做的用例。

+0

我试着这个,并保持张贴,谢谢。 –