简体   繁体   English

使用自定义类型Angular验证输入

[英]Validate Input with custom type Angular

I have a component with an Input defined as a custom type. 我有一个输入定义为自定义类型的组件。

@Input() public labelSize: 'small' | 'normal' | 'large'  = 'normal';

But apparently I can pass any kind of parameter to the component tag. 但是显然我可以将任何类型的参数传递给component标签。 I will never have any kind of error/warning. 我永远不会有任何错误/警告。

<my-component labelSize="whatever"></my-component>

I can event pass a number 我可以通过电话号码

<my-component labelSize=12345></my-component>

I expected the typescript compiler or angular to give me some feedback on this kind of error. 我希望打字稿编译器或angular可以给我一些有关此类错误的反馈。

I'm supposed to validate myself the type of all the Inputs of my components? 我应该验证自己所有组件的输入类型吗?

Any best practices? 有最佳做法吗?

Thanks 谢谢

The angular templates are HTML and are not in any way hooked into typescript for checking this. 角度模板是HTML,没有以任何方式挂钩到打字稿中进行检查。 And even in typescript it's allowed to bypass the type declaration, eg this.labelSize = 'whatever' as any . 甚至在打字稿中,也可以绕过类型声明,例如this.labelSize = 'whatever' as any

In the end the code is still javascript. 最后,代码仍然是javascript。 And in the templates is just like using plain javascript from the start. 在模板中,就像从一开始就使用纯JavaScript一样。

If you really want to catch mismatches up front, some possible solutions are: 如果您确实想提前发现不匹配的情况,则可能的解决方案是:

1. Validation 1.验证

As already suggested, do a manual validation or use a validation library to specify constraints, eg https://validatejs.org/ 如已经建议的,执行手动验证或使用验证库来指定约束,例如https://validatejs.org/

By the way, you can also use a Pipe to add validation on the fly on any of your values and have more clarity on your html. 顺便说一句,您还可以使用Pipe在任何值上即时添加验证,并在html上更加清晰。

2. Config objects 2.配置对象

You can capture the configuration of components where types are important in an object, as such 您可以捕获类型在对象中很重要的组件的配置,例如

@Input() public config: {
  labelSize: 'small' | 'normal' | 'large';
} = { labelSize: 'normal' }

and then bind the input to myCompConfig : 然后将输入绑定到myCompConfig

<my-component [config]="myCompConfig"></my-component>

then in your controller where you use it 然后在您使用它的控制器中

this.myCompConfig = { labelSize: 'whatever' } // error <- type is enforced now

3. TS for templates 3.模板的TS

You can use TS instead of HTML for templates, and assist it with some type info: 您可以将TS而不是HTML用于模板,并通过一些类型信息来辅助它:

Share your type first 首先分享您的类型

export type LabelSize = 'small' | 'normal' | 'large'

@Input() public labelSize: LabelSize = 'normal';

my-template.ts 我的模板

const labelSize: LabelSize = 'whatever' // error <- type is enforced 

export const template = `
    <my-component labelSize=${labelSize}></my-component>`
`;

then in your component use it directly 然后在您的组件中直接使用它

import { template } from './my-template.ts';
@Component({ selector: 'something', template })

Note this can also be extracted into a factory-approach for creating parts of your temples, eg you can have a factory for creating this element based on a labelSize param (and this param can have type info). 请注意,这也可以提取到用于创建寺庙部分的工厂方法中,例如,您可以拥有一个工厂,用于基于labelSize参数创建此元素(并且该参数可以具有类型信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM