5
我有這段代碼,無論我嘗試,我無法通過以下錯誤。TypeScript'...'類型'typeof'上不存在''
錯誤: 屬性'EmailValidator'在類型'typeof UserValidators'上不存在。
代碼:
import {EMAIL_REGEX} from '../constants';
import {Control} from 'angular2/common';
export interface IUserValidators {
EmailValidator(control: Control) : Object;
}
export class UserValidators implements IUserValidators {
EmailValidator(control: Control) : Object {
if (!control.value) {
return {
required: true
};
} else if (control.value) {
if (!new RegExp(EMAIL_REGEX).test(control.value)) {
return {
invalid: true
};
}
}
return {};
}
}
這是我嘗試注入爲EmailValidator:
this.fb.group({
email: ['', UserValidators.EmailValidator]
});
該錯誤是完全正確的。這是一種實例方法;你需要一個實例。 – SLaks
謝謝SLaks,我忘了它。 – alik