bur*_*1ce 2 typescript angular
我是 TypeScript 和 Angular 的新手,但我在 C# 方面有很好的背景。
我正在阅读这篇文章,特别是“自定义验证器”和这个片段
export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} | null => {
const forbidden = nameRe.test(control.value);
return forbidden ? {'forbiddenName': {value: control.value}} : null;
};
}
Run Code Online (Sandbox Code Playgroud)
这行代码是什么/意味着什么?这是 TypeScript 定义委托的方式吗?
(control: AbstractControl): {[key: string]: any} | null => {
Run Code Online (Sandbox Code Playgroud)
这是一个arrow function. 要理解它,您可以将其阅读为
function (control: AbstractControl): {[key: string]: any} | null {
const forbidden = nameRe.test(control.value);
return forbidden ? {'forbiddenName': {value: control.value}} : null;
};
Run Code Online (Sandbox Code Playgroud)
也就是说,函数返回类型{[key: string]: any}(一个对象,其键被声明为名为 的变量key,值类型可以是任何),或null;而该函数需要 Angular 类型的输入值AbstractControl。
在 TypeScript 中,冒号经常以两种方式使用:
{ key: value }希望这可以帮助。欢迎来到 Angular 和 TypeScript 的世界!
| 归档时间: |
|
| 查看次数: |
1392 次 |
| 最近记录: |