Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular TypeError无法设置未定义的属性_Angular_Typescript - Fatal编程技术网

Angular TypeError无法设置未定义的属性

Angular TypeError无法设置未定义的属性,angular,typescript,Angular,Typescript,我得到一个类型错误:无法为以下代码行设置未定义的属性“error”:this.error=error.code 下面是我试图做的(它是一种角度反应形式,调用web服务): @组件({ // ... }) 导出类SignUpComponent实现OnInit{ //更多声明。。。 错误:string | null;//我还尝试了error=''和error=null //建造师等。 公开提交(){ this.error=null; if(this.signUpForm.valid){ //创建一

我得到一个
类型错误:无法为以下代码行设置未定义的属性“error”:
this.error=error.code

下面是我试图做的(它是一种角度反应形式,调用web服务):

@组件({
// ...
})
导出类SignUpComponent实现OnInit{
//更多声明。。。
错误:string | null;//我还尝试了error=''和error=null
//建造师等。
公开提交(){
this.error=null;
if(this.signUpForm.valid){
//创建一个Auth0用户
this.authService.signUp(this.signUp.value.email、this.signUp.value.password、this.authServiceCallback);
}
}
公共authServiceCallback(错误){
如果(错误){
//这将导致出现错误子组件
this.error=error.code;
}否则{
//做点别的
}
}
}
使用

否则,
将不会指向当前类实例


另请参见

您也可以通过箭头功能方式执行此操作:

this.authService.signUp(this.signUp.value.email, this.signUp.value.password, x => this.authServiceCallback ( x (or whatever) ) );
箭头函数使“this”更可预测

如果不需要参数,则设置格式:

() => this.authServiceCallback ( )

非常感谢。我接受甘特的答案只是因为它是在几秒钟前出现的。
() => this.authServiceCallback ( )