为什么`@ input`装饰器要优先于`inputs:[]`

问题描述:

有两种方法可以在组件上定义输入:

There are two ways to define an input on a component:

@Component({
    inputs: ['displayEntriesCount'],
    ...
})
export class MyTable implements OnInit {
    displayEntriesCount: number;

还有这个

@Component({
   ...
})
export class MyTable implements OnInit {
    @Input() displayEntriesCount: number;

我认为第一种方法更好,因为它显式声明了组件的依赖关系,而无需检查补码类.但是,著名开发人员本文指出,第二种方法是最好:

I would assume that the first approach is better since it explicitly declares component's dependencies without the need to inspect complements class. However, this article by the renowned developer states that the second approach is preferable:

使用@Input是首选方法,但是我们不必使用它.

Using @Input is a preferred approach, however we don’t have to use it.

有什么想法吗?

每个角度样式指南 内联装饰输入和输出属性

请使用@Input和@Output代替输入和输出属性 @Directive和@ Component`装饰器的组成:

Do use @Input and @Output instead of the inputs and outputs properties of the @Directive and@Component` decorators:

将@Input()或@Output()与属性放在同一行 他们装饰.

Do place the @Input() or @Output() on the same line as the property they decorate.

  • 为什么?识别类中的哪些属性是输入或输出更加容易且更具可读性.

    • Why? It is easier and more readable to identify which properties in a class are inputs or outputs.

      为什么?如果您需要重命名与@Input或@Output关联的属性或事件名称,则可以在单个位置进行修改.

      Why? If you ever need to rename the property or event name associated with @Input or @Output, you can modify it a single place.

      为什么?该指令附带的元数据声明更短,因此可读性更高.

      Why? The metadata declaration attached to the directive is shorter and thus more readable.

      为什么?将装饰器放在同一行上可以缩短代码长度,并且仍然可以轻松地将该属性标识为输入或输出.

      Why? Placing the decorator on the same line makes for shorter code and still easily identifies the property as an input or output.