本文介绍了如何连接Output()和Input()装饰器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为生成JSON的组件创建一个接口。我想强制每个实现组件接受类型作为输入并产生输出:

I want to create an interface for components that generate JSON. I want to force each implementing component to accept a type as Input and produce an Output:

import { EventEmitter, Output, Input } from '@angular/core';
import { Foo, FooConfiguration } from '../../interfaces';
interface FooConfigurator {
    @Output() fooWasConfigured: EventEmitter<FooConfiguration>;
    @Input() fooInstance: Foo;
}

然后,实现FooConfigurator的组件将确保以下各项:

Then, components implementing FooConfigurator would ensure the following:

import { EventEmitter, Output, Input } from '@angular/core';
import { Foo, FooConfiguration, FooConfigurator } from '../../interfaces';
class ConcreteFooConfigurator implements FooConfigurator {
    @Output() fooWasConfigured: EventEmitter<FooConfiguration>;
    @Input() fooInstance: Foo; 
}

此接口定义失败,因为它是无效的语法。我该怎么做,或者更好地解决该问题?

This interface definition fails because it is invalid syntax. How can I do it, or better approach the problem?

推荐答案

目前不可能将装饰器与TypeScript接口。第二种最好的方法是简单地键入类型并添加有关它的注释。

It is impossible presently to interface decorators with TypeScript. The next best way is to simply interface the types and add comments about it.

interface FooConfigurator {
    fooWasConfigured: EventEmitter<FooConfiguration>;
    fooInstance: Foo;
}

这基本上可以满足需求,EventEmitter看起来确实可靠应该发出一个事件,并且在fooInstance中指示类具有这样的属性。但是,如何应使用它们位于注释领域。

This in essence pretty much covers the need, the EventEmitter will reliably looks like it should emit an event, and in fooInstance instructs the class to have such an a property. How these should be used resides in the comments realm however.

这篇关于如何连接Output()和Input()装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 22:22