Why is `@input` decorator preferred over `inputs:[]`

前端 未结 2 1579
轮回少年
轮回少年 2021-01-21 10:23

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

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


        
相关标签:
2条回答
  • 2021-01-21 10:28

    Per angular style guide https://angular.io/docs/ts/latest/guide/style-guide.html#!#-a-id-05-12-a-decorate-input-and-output-properties-inline

    Decorate Input and Output Properties Inline

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

    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.

    • 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.

    0 讨论(0)
  • 2021-01-21 10:40

    It's somehow preferred by the style guide, but there are no strong arguments. Perhaps because the properties and methods are kept together with the binding. But your argument also is valid. Some prefer the one, some the other.

    If you prefer host: ... then just use it. It's still quite common.

    0 讨论(0)
提交回复
热议问题