angular - util方法的静态字段或const函数?
问题描述
我需要创建一组可重用的验证器。
将它们声明为静态常量函数会更好吗?
export const emailValidator: ValidatorFn =
function(control: AbstractControl): {[key: string]: any} | null {
return [...];
};
export const anotherValidator: ValidatorFn =
function(control: AbstractControl): {[key: string]: any} | null {
return [...];
};
还是作为不可实例化类的静态方法?
class CustomValidators {
static emailValidator =
function(control: AbstractControl): {[key: string]: any} | null {
return [...];
}
static anotherValidator =
function(control: AbstractControl): {[key: string]: any} | null {
return [...];
}
}
第二个解决方案看起来不错,因为我将能够使用CustomValidators.*
和自动完成,但创建一个实际上永远不会被实例化的类似乎“丑陋”。我更多地将它用作命名空间。
另外,它对摇树有影响吗?未使用的验证器会在构建时被抑制吗?
解决方案
静态对象将始终保留在 javascript 发出的包中,以便随时用作对象。如果你愿意,你可以在 util 函数中编写那些可以按需创建验证器而不是将它们预初始化为静态对象的函数。另一种方法是创建命名空间并按需初始化。
推荐阅读
- javascript - HTML 中的井字棋盘
- typescript - 如何区分打字稿中的日期?
- javascript - 组件正在更改要控制的非受控输入。这可能是由于值从未定义更改为已定义值
- kubernetes - 我需要部署什么版本的软件才能获得 kubernetes 1.19.7
- windows - 如何从损坏的地方恢复 WinSCP 中的文件传输
- python - 尝试在集合中查找项目时出现 Python mongodb/motor“'ObjectId' 对象不可迭代”错误
- vim - 如何在 Vim 映射/过滤器中使用扩展修饰符?
- r - 为ggplot2自动选择不同的可见颜色
- docker - 带有远程 ssh 的 docker-compose 上下文不起作用
- icloud - watchOS 的 NSUbiquitousKeyValueStore 等效项是什么?