Ant*_*huk 10 typescript angular
有时我们可以有一个案例,应该省略泛型变量.像这样:
@Component( ... )
class MyComponent {
@Output()
public cancel = new EventEmitter<undefined>();
private myFoo() {
this.cancel.emit(); // no need to pass any value
}
}
Run Code Online (Sandbox Code Playgroud)
所以,问题是:哪种方法更好地定义EventEmitter类型:
EventEmitter<undefined>或EventEmitter<void>.
void更好,因为在.emit()电话中没有争论. undefined更好.emit()是一样的.emit(undefined)你有什么意见?
Joe*_*lay 16
根据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);
}
}
Run Code Online (Sandbox Code Playgroud)
然而EventEmitter<undefined>,你只能通过undefined或没有参数,这可能在你的情况下更正确 - 说,我看不出任何重大问题只是因为你传递null给你不期望值的发射器无论如何,所以我很想选择,void因为它是较短的选择.
| 归档时间: |
|
| 查看次数: |
3685 次 |
| 最近记录: |