angular - Angular 7 错误 RangeError:超出最大调用堆栈大小
问题描述
我正在尝试通过遵循官方教程hero component
来学习角度,但是当按照and的步骤进行操作时hero detail component
,它会引发错误“RangeError:超出最大调用堆栈大小”。
hero.component.html
:_
<ul class="heroes">
<li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>
<!--
<app-hero-detail [hero]="selectedHero"></app-hero-detail> -->
<app-heroes></app-heroes>
detail component
:_
<div *ngIf="hero">
<h2>{{hero.name}} Details</h2>
<div><span>id: </span>{{hero.id}}</div>
<div>
<label>name:
<input [(ngModel)]="hero.name" placeholder="name"/>
</label>
</div>
</div>
<app-hero-detail [hero]="selectedHero"></app-hero-detail>
这hero component
import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';
import { HeroService } from '../hero.service';
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
heroes: Hero[];
selectedHero: Hero;
constructor(private heroService: HeroService) { }
ngOnInit() {
this.getHeroes();
}
getHeroes(): void {
this.heroes = this.heroService.getHeroes();
}
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
}
这hero.detail component
import { Component, OnInit, Input } from '@angular/core';
import { Hero } from '../hero';
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html',
styleUrls: ['./hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {
@Input() hero: Hero;
constructor() { }
ngOnInit() {
}
}
值得一提的是,当<app-heroes></app-heroes>
被评论时,列表页面加载时没有错误
解决方案
1.出现死循环时出现此错误。正如您所提到的,当app-heroes被评论时页面加载,这可能被用作多个组件的选择器名称,这是不允许的。这可能会导致无限循环并且无法加载组件。
- 尝试进行以下编辑,
hero.component.html
<ul class="heroes">
<li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>
<app-hero-detail [hero]="selectedhero"></app-hero-detail>
hero.detail.component.html
<div *ngIf="hero">
<h2>{{hero.name}} Details</h2>
<div><span>id: </span>{{hero.id}}</div>
<div>
<label>name:
<input [(ngModel)]="hero.name" placeholder="name"/>
</label>
</div>
</div>
希望这可以帮助。
推荐阅读
- react-native - 如何在屏幕上导入 defaultNavigationOptions 属性?
- sql-server - 将数据库从 SQL Azure 导入到 localDB 时出错
- python - 根据 DataFrame 从 DataFrames 列表中删除 DataFrame - python?
- stream - 无法理解/可视化 SICP 流 Hamming 数字程序
- azure - 插入具有相同分区键值和不同“id”值的文档
- ruby-on-rails - 处理 Rails 5 会话
- java - 每个 API 的 Firestore 导入/导出
- excel - 在 Excel 中重新标记大量数据
- php - 我如何通过 on-Change 或从下拉列表中单击或通过开关按钮(如开/关)更新数据库
- reactjs - React Hooks:尝试在卸载返回初始状态之前访问状态