简体   繁体   English

为什么在运行时未定义我的构造函数参数私有类成员?

[英]Why is my constructor parameter private class member undefined at run-time?

I am running into an issue where my private class variable is undefined at run-time. 我遇到一个问题,在运行时我的私有类变量未定义。 The code is: 代码是:

export class AdminRegistrationComponent {

    adminRegistrationForm:ControlGroup;
    usernameValidation:FormValidation;

    constructor(private fb:FormBuilder) {

        this.usernameValidation = new FormValidation(AdminRegistrationValidations.username);

        this.adminRegistrationForm = fb.group({
            username: new Control("", this.usernameValidation.valid)
        });
    }

    submit() {
        console.log(this.adminRegistrationForm);
    }
}

and FormValidation : FormValidation

export class FormValidation {

    constructor(private regex:RegExp) {
    }

    valid(control:Control):ValidationResult {

        if (this.regex.test(control.value)) {
            return null;
        }

        return {"valid”": true};
    }

    get():string {
        return this.regex.toString().slice(1, -1);
    }
}

The issue is when valid is called on this.usernameValidation the private regex variable in FormValidation is undefined at run-time (I have confirmed the right value is being passed in). 问题是有效的,当被要求this.usernameValidation私营正则表达式变量FormValidation在运行时是不确定的(我已确认正确的价值正在被传递)。 I have read that in Angular2 there are conditions around dependency injection that need to be considered, but I can't get anything to work. 我已经读到在Angular2中有一些需要考虑的依赖注入条件,但是我什么都无法工作。 Essentially I have tried listing the classes as @Injectable , among other similar things. 本质上,我尝试过将类列为@Injectable ,以及其他类似的东西。 The exact error I am getting is: 我得到的确切错误是:

EXCEPTION: TypeError: Cannot read property 'test' of undefined

Thanks 谢谢

Final Edit 最终编辑

The answer as noted below was that my valid function needed to be an arrow function: 如下所述,答案是我的有效函数必须是箭头函数:

valid = (control:Control):ValidationResult => {

    if (this.regex.test(control.value)) {
        return null;
    }

    return {"valid”": true};
}

he private regex variable in FormValidation is undefined at run-time (I have confirmed the right value is being passed in). 他在运行时未定义FormValidation中的私有正则表达式变量(我已经确认传递了正确的值)。

Most likely regex is undefined: regex很可能是未定义的:

constructor(private regex:RegExp) {
}

If not then valid(control:Control):ValidationResult { is being called with the wrong this . 如果不是,则使用错误的this调用valid(control:Control):ValidationResult { Fix use an arrow: 修复使用箭头:

valid = (control:Control):ValidationResult => {

    if (this.regex.test(control.value)) {
        return null;
    }

    return {"valid”": true};
}

More on arrow functions : https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html 有关箭头功能的更多信息: https : //basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

Video on this in TypeScript 视频this的打字稿

It's a problem with JavaScript itself. JavaScript本身就是一个问题。 In fact, when referencing a function, you need to bind to an object to be able to use the this keyword. 实际上,在引用函数时,您需要绑定到对象才能使用this关键字。

Using the bind method should fix your problem: 使用bind方法应该可以解决您的问题:

constructor(private fb:FormBuilder) {
  this.usernameValidation = new FormValidation(
              AdminRegistrationValidations.username);

  this.adminRegistrationForm = fb.group({
    username: new Control("", 
        this.usernameValidation.valid.bind(this.usernameValidation)
  });
}

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

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