我可以将变量作为值分配给对象的键吗?
Can I assign a variable as value to an object's key?
这允许吗?
export class H{
passwordErrorMessage = 'Password must contain 1 small-case alphabet, 1 capital alphabet, 1 digit, 1 special character. The length should be 6-10 characters.'
...
validatePassword(control: FormControl) {
...
return (REG_EXP.test(password)) ? null : {
validatePassword: { // check the class ShowErrorsComponent to see how validatePassword is used.
valid: false,
message: this.passwordErrorMessage //can I do this?
}
};
}
}
对于我的一个测试用例,我收到以下错误
TypeError: Cannot read property 'passwordErrorMessage' of undefined
Error object: Property name: ngDebugContext, value: [object Object]
Error object: Property name: ngErrorLogger, value: function () { [native code] }
TypeError: Cannot read property 'passwordErrorMessage' of undefined
at HelperService.validatePassword (webpack:///./src/app/helper.service.ts?:224:31)
好像this
是undefined
。我仍处于调试的早期阶段,但我的第一个疑问是 this
的用法是否正确?如果我将用法更改为 message: 'Password must contain 1 small-case alphabet, 1 capital alphabet, 1 digit, 1 special character. The length should be 6-10 characters.'
,一切正常
可以,但是您需要将 validatePassword
绑定到 class 或使用箭头函数将 this
上下文传递给函数。这应该有效:
export class H{
passwordErrorMessage = 'Password must contain 1 small-case alphabet, 1 capital alphabet, 1 digit, 1 special character. The length should be 6-10 characters.'
...
validatePassword = (control: FormControl) => {
...
return (REG_EXP.test(password)) ? null : {
validatePassword: { // check the class ShowErrorsComponent to see how validatePassword is used.
valid: false,
message: this.passwordErrorMessage
}
};
}
}
这允许吗?
export class H{
passwordErrorMessage = 'Password must contain 1 small-case alphabet, 1 capital alphabet, 1 digit, 1 special character. The length should be 6-10 characters.'
...
validatePassword(control: FormControl) {
...
return (REG_EXP.test(password)) ? null : {
validatePassword: { // check the class ShowErrorsComponent to see how validatePassword is used.
valid: false,
message: this.passwordErrorMessage //can I do this?
}
};
}
}
对于我的一个测试用例,我收到以下错误
TypeError: Cannot read property 'passwordErrorMessage' of undefined
Error object: Property name: ngDebugContext, value: [object Object]
Error object: Property name: ngErrorLogger, value: function () { [native code] }
TypeError: Cannot read property 'passwordErrorMessage' of undefined
at HelperService.validatePassword (webpack:///./src/app/helper.service.ts?:224:31)
好像this
是undefined
。我仍处于调试的早期阶段,但我的第一个疑问是 this
的用法是否正确?如果我将用法更改为 message: 'Password must contain 1 small-case alphabet, 1 capital alphabet, 1 digit, 1 special character. The length should be 6-10 characters.'
可以,但是您需要将 validatePassword
绑定到 class 或使用箭头函数将 this
上下文传递给函数。这应该有效:
export class H{
passwordErrorMessage = 'Password must contain 1 small-case alphabet, 1 capital alphabet, 1 digit, 1 special character. The length should be 6-10 characters.'
...
validatePassword = (control: FormControl) => {
...
return (REG_EXP.test(password)) ? null : {
validatePassword: { // check the class ShowErrorsComponent to see how validatePassword is used.
valid: false,
message: this.passwordErrorMessage
}
};
}
}