Angular 2中的自定义重复密码验证错误

Angular 2中的自定义重复密码验证错误,angular,typescript,angular-validation,Angular,Typescript,Angular Validation,我正在参考scotch.io网站学习Angular2注册表单重复密码的自定义验证: 以下是我的validator.compare.ts代码: import {AbstractControl} from '@angular/forms'; export class PasswordValidation { static MatchPassword(AC: AbstractControl) { let password = AC.get('password').value;

我正在参考scotch.io网站学习Angular2注册表单重复密码的自定义验证:

以下是我的validator.compare.ts代码:

import {AbstractControl} from '@angular/forms';
export class PasswordValidation {

    static MatchPassword(AC: AbstractControl) {
       let password = AC.get('password').value; // to get value in input tag
       let confirmPassword = AC.get('confirmPassword').value; // to get value in input tag
        if(password != confirmPassword) {
            console.log('false');
            AC.get('confirmPassword').setErrors( {MatchPassword: true} )
        } else {
            console.log('true');
            return null
        }
    }
}
及注册

import { OnInit, Component } from "@angular/core";
import { UserModel } from "../../_model/usermodel";
import { FormGroup, FormControl, Validators } from "@angular/forms";
import { PasswordValidation } from '../../_utilities/_validations/validator.compare';

@Component({
    templateUrl: './_componant/registration/registration.html'
})

export class RegistrationComponent implements OnInit {
    user: UserModel = { id: 0, email: "", password: "", username: "" };
    myform : FormGroup;
    submitted : boolean = false;

    ngOnInit(): void {
        this.myform = new FormGroup({
            username : new FormControl("", Validators.required),
            password : new FormControl("", Validators.required),
            confirmPassword: new FormControl("", Validators.required)
        },{ validator: PasswordValidation.MatchPassword })
    }
    constructor() {
    }
}
我遇到以下错误:

类型为“{validator:(AC:AbstractControl)=>any;}”的参数不能分配给类型为“ValidatorFn”的参数。 对象文字只能指定已知属性,并且“validator”类型中不存在“validator fn”

为了更清晰,我附上了截图。

以下是路径的项目文件夹层次结构:
试试看

import { PasswordValidation } from 'YOUR/CLASS/PATH/_utilities/_validations/validator.compare';

    this.myform = new FormGroup({
                username : new FormControl("", Validators.required),
                password : new FormControl("", Validators.required),
                confirmPassword: new FormControl("", Validators.required)
            },{ validator: MatchPassword });
错误表明您没有指向正确的方法, 如果路径正确,只需调用该函数。

试试看

import { PasswordValidation } from 'YOUR/CLASS/PATH/_utilities/_validations/validator.compare';

    this.myform = new FormGroup({
                username : new FormControl("", Validators.required),
                password : new FormControl("", Validators.required),
                confirmPassword: new FormControl("", Validators.required)
            },{ validator: MatchPassword });
错误表明您没有指向正确的方法,
如果路径正确,只需调用函数。

您必须检查如何构建验证器fn


定义方法的返回类型。

您必须检查如何构建验证器fn


定义方法的返回类型。

我附加了文件夹层次结构以了解路径。我想我写的路径是正确的,是吗?我附加了文件夹层次结构来理解路径。我想我写的是正确的路径,是吗?