首页 > 解决方案 > 有什么作用?在 Angular 函数参数中是什么意思?

问题描述

在浏览 Angular 4 教程时,我发现了一种在函数中获取/设置参数的新方法。

onValueChange(data?: any)

做什么的?

标签: angulartypescript

解决方案


?符号表示optional。这意味着如果您不将值传递给函数,它将不会抛出error.

例如:

这是你的功能

onValueChange(data?: any) {
  console.log(data);
}
onValueChange('somedata'); // will print 'somedata' in the console
onValueChange(); // will print undefined but it won't throw an error

摘要:您可以在不传递值的情况下调用此函数,因为它是可选的。


推荐阅读