繁体   English   中英

错误类型错误:无法使用 Angular 读取 Object.eval [as updateRenderer] 处未定义的属性“codeSprint”

[英]ERROR TypeError: Cannot read property 'codeSprint' of undefined at Object.eval [as updateRenderer] using Angular

有人可以解释一下可能出了什么问题吗? 我收到此错误

ERROR TypeError: Cannot read property 'codeSprint' of undefined
    at Object.eval [as updateRenderer] (SprintComponent.html:12)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:45294)
    at checkAndUpdateView (core.js:44277)

在 SprintComponent.html 中:

<div ngFor="let sprint of sprints">
    {{sprint.codeSprint}}
    {{sprint.title}}
  </div>

在 SprintComponent.ts 中:

sprints : Sprint[];

在 SprintService.ts 中:

getSprints(): Observable<Sprint[]> {
    const token = localStorage.getItem('kno');
    const rqwstrId = localStorage.getItem('rqwstrId');
    let header = new HttpHeaders('Content-Type: application/json; charset=UTF-8'); 
    header = header.append('kno',token);
    header = header.append('rqwstrId', rqwstrId);
    let param = new HttpParams().set('code',"Koon52");
    return this.http.get<Sprint[]>(this.baseUrl + '/v1/sprints/', {headers : header , params : param});
  }

将 Sprint 作为具有以下属性的类:

export class Sprint {
  kno:string;
  rqwstrId : string;
  code: string;
  codeSprint : string;
  dateDebut : string;
  dateFin : string;
  title : string;

  constructor() {
  }
}

同时,当我将其登录到控制台时,它会显示如下数据:

SprintComponent.html:9 ERROR TypeError: Cannot read property 'codeSprint' of undefined
    at Object.eval [as updateRenderer] (SprintComponent.html:9)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:45294)
    at checkAndUpdateView (core.js:44277)
    at callViewAction (core.js:44637)
    at execComponentViewsAction (core.js:44565)
    at checkAndUpdateView (core.js:44278)
    at callViewAction (core.js:44637)
    at execEmbeddedViewsAction (core.js:44594)
    at checkAndUpdateView (core.js:44272)
    at callViewAction (core.js:44637)

我像这样在 SprintComponent 中调用 getSprints 方法:

ngOnInit() : void {
    this.sprintService.getSprints().subscribe(
      data =>
      {
          this.sprints = data;
      }
    );

谢谢!

您需要订阅组件中的方法

sprints:any[]=[];
 in your ngoninit 

     this.service.getSprints().subscribe((response) => {
            this.sprints=response as any 
            });
    
    )}

在你的 component.html

<div *ngFor="let sprint of sprintd">
 {{sprint.codeSprint}}
    {{sprint.title}}
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM