I am trying to create a ValidatorFn that will be used in several components which represent custom reactive form controls. My initial idea was to generate a TypeScript class, create a public static function (or several) there and then just use it like so: myFGroup.setValidators(ReactiveValidators.myValidator).
The problem is, although I imported this ReactiveValidators class in the app.module and added it to declarations: [], I still get the favorite error:
error TS2307: Cannot find module 'src/app/validators/reactive-validators'
Can someone tell me what I am doing wrong? Also, is this approach even a good one at all?
@Componentinside yourReactiveValidatorsclass ? Did you addReactiveValidatorsin a module'sdeclaration? If so, remove it. It is not an Angular Component. - RandommyFGroup.setValidators(ReactiveValidators.myValidator)will work, whereReactiveValidatorsis in its own file anywhere in your app. - RandomReactiveValidatorsclass usingng generate class, but I still get the error above. - dzenesizimport { maxInputLengthValidator } from '../../validators/reactive-validators'. Notice the relative path. And, for some reason, VS Code auto import uses absolute path, the one in the error message. I fixed the path, now it works. Thanks for the help! - dzenesiz