加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – EventEmitter和EventEmitter之间有什么区别?

发布时间:2020-12-17 17:57:10 所属栏目:安全 来源:网络整理
导读:有时我们可以有一个案例,应该省略泛型变量. 像这样: @Component( ... )class MyComponent { @Output() public cancel = new EventEmitterundefined(); private myFoo() { this.cancel.emit(); // no need to pass any value }} 所以,问题是:哪种方法更好地
有时我们可以有一个案例,应该省略泛型变量.
像这样:

@Component( ... )
class MyComponent {

  @Output()
  public cancel = new EventEmitter<undefined>();

  private myFoo() {
    this.cancel.emit(); // no need to pass any value
  }
}

所以,问题是:哪种方法更好地定义EventEmitter类型:
EventEmitter<未定义>或EventEmitter< void>.

> void更好,因为.emit()调用中没有参数.
> undefined更好.emit()是相同的.emit(undefined)

你有什么意见?

解决方法

根据TypeScript文档,void类型接受undefined和null – 因此,以下代码有效:

@Component( ... )
class MyComponent {

  @Output()
  public cancel = new EventEmitter<void>();

  private myFoo() {
    this.cancel.emit();
    this.cancel.emit(undefined);
    this.cancel.emit(null);
  }
}

而使用EventEmitter< undefined>,你只能传递未定义或没有参数,这在你的情况下可能更正确 – 说,我看不到任何重大问题只是因为你传递null给你的发射器不管怎样都不期待价值,所以我很想选择无效,因为它是较短的选择.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读