gpt4 book ai didi

Angular 7 错误 RangeError : Maximum call stack size exceeded

转载 作者:太空狗 更新时间:2023-10-29 17:14:15 24 4
gpt4 key购买 nike

我正在尝试通过遵循 official tutorial 来学习 Angular但是当执行 hero component 的步骤时和 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>有注释,列表页加载无误

Error message screenshot感谢任何帮助

最佳答案

1.出现死循环时出现该错误。正如您提到的,当 app-heroes 被评论时页面加载,这可能被用作多个组件的选择器名称,这是不允许的。这可能会导致无限循环并无法加载组件。

  1. 尝试进行以下修改,

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>

希望这对您有所帮助。

关于 Angular 7 错误 RangeError : Maximum call stack size exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53756451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com