这个错误通常是由于 TypeScript 版本的原因造成的。在 Angular 13 中,表单验证需要使用 Angular 的内置验证器以及自定义验证器。当使用自定义验证器时,需要将错误消息字符串返回为数组,而不是一个字符串。
例如,在一个自定义验证器中:
export const customValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
const value = control.value;
if (!value) {
return null;
}
const containsNumber = /[0-9]/.test(value);
return containsNumber ? null : {customError: ['error message']};
};
可以看到,在返回自定义错误消息时,使用的是数组形式。如果使用字符串形式,则会出现“意外的令牌[”错误。
通过修改代码,将自定义错误消息从字符串改为数组,即可解决该问题。
上一篇:Angular13项目中使用的angularbootstrap出现错误。
下一篇:Angular13显示ContentandMapofthisSourceisnotavailable(onlysize()issupported)错误