【问题标题】:What is the difference between EventEmitter<Example> and EventEmitter<Example>()EventEmitter<Example> 和 EventEmitter<Example>() 有什么区别
【发布时间】:2019-12-30 17:32:53
【问题描述】:

两者之间有什么区别(如果有的话)

@Output() exampleChange: EventEmitter&lt;Example&gt; = new EventEmitter();

@Output() exampleChange = new EventEmitter&lt;Example&gt;();

【问题讨论】:

    标签: angular angular-event-emitter


    【解决方案1】:

    没有区别,实际上是一样的。

    这定义了类型然后初始化它:

    @Output() exampleChange: EventEmitter<Example> = new EventEmitter();
    

    这是直接初始化的:

    @Output() exampleChange = new EventEmitter<Example>();
    

    我推荐使用第二种方式

    【讨论】:

      【解决方案2】:

      在组件中设置变量时考虑一下

      export class MyTestComponent {
        myNumber:number = 10;
      }
      -------------------------------------
      export class MyTestComponent {
        myNumber = 10;
      }
      

      在这两个例子中,我创建了一个调用 myNumber 的变量并将其值设置为 10, 但在第二个中,我给了它一个类型。 在 JS 中除了脚本的顺序好,代码没有区别

      【讨论】:

        猜你喜欢
        • 2018-04-06
        • 2016-09-10
        • 2020-06-18
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        相关资源
        最近更新 更多