Angular 错误范围错误:超过最大调用堆栈大小[角度]
我是Angular的完全初学者,只编写了大约2年或更少的代码。我正在构建一个待办事项列表应用程序来学习。目前,我正在创建一个表单(登录)并遵循文档。不幸的是,当我用表单的html修改app.component.html时,我得到了错误RangeError:超过了最大调用堆栈大小。我不知道为什么会出现这个错误。我搜索过论坛,我看到的唯一可能性是可能我有一个无限循环,尽管考虑到我没有在代码中实现任何循环,我不知道这是怎么可能的。我看到有人问过这个问题,但从来没有人回答过。任何帮助都将不胜感激 请参阅下面的代码或查看我的GitHub 同一个从未被回答过的问题- user.tsAngular 错误范围错误:超过最大调用堆栈大小[角度],angular,Angular,我是Angular的完全初学者,只编写了大约2年或更少的代码。我正在构建一个待办事项列表应用程序来学习。目前,我正在创建一个表单(登录)并遵循文档。不幸的是,当我用表单的html修改app.component.html时,我得到了错误RangeError:超过了最大调用堆栈大小。我不知道为什么会出现这个错误。我搜索过论坛,我看到的唯一可能性是可能我有一个无限循环,尽管考虑到我没有在代码中实现任何循环,我不知道这是怎么可能的。我看到有人问过这个问题,但从来没有人回答过。任何帮助都将不胜感激 请参阅
export class User {
constructor(
public username: string,
public password: string
) {
let user = new User('aaa',
'a');
console.log('My username is ' + user.username);
}
}
login-form.component.ts
import { Component, OnInit } from '@angular/core';
import { User } from '../user';
@Component({
selector: 'app-login-form',
templateUrl: './login-form.component.html',
styleUrls: ['./login-form.component.css']
})
export class LoginFormComponent {
model = new User('Tester1', '12345');
submitted = false;
onSubmit() { this.submitted = true; }
// TODO: Remove this when we're done
get diagnostic() { return JSON.stringify(this.model); }
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { LoginFormComponent } from './login-form/login-form.component';
@NgModule({
declarations: [
AppComponent,
LoginFormComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html
<app-login-form></app-login-form>
当输入上述行时,代码中断。问题出在
用户
类中
export class User {
constructor(
public username: string,
public password: string
) {
//this is where user constructor is getting called
let user = new User('aaa', 'a');
console.log('My username is ' + user.username);
}
}
您基本上是在自己的构造函数中创建User
类的新实例
这会导致调用用户
构造函数和创建用户
实例的循环过程,最终导致angular应用程序崩溃,出现错误范围错误:超过最大调用堆栈大小[angular]
错误
示例代码
类用户{
构造函数(){
log(“构造函数调用!!”);
设a=新用户();
}
}
新用户()代码>听起来可能很傻,但我无意中将组件包装在了自己的HTML标记中。这导致了错误。我只是想添加我自己的情况,这可能会帮助其他人。。。我遇到了同样的错误,结果是我将我的nav组件的选择器命名为“nav”,但我的html代码也使用了nav标记作为引导标记。这导致了一个无意的循环。一旦我重新命名了选择器,问题就消失了