作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Angular 8 应用程序。如果加载某个组件,我会收到错误
无法读取未定义的属性“长度”
我用谷歌搜索了很多并做了一些教程。但我不知道如何处理这个错误。
所以如果我加载这个组件:
import { Component, OnInit } from '@angular/core';
import { HealthAPIService } from '../../shared/health-api/health-api.service';
import { DossierEntry } from '../../interfaces/dossier/dossier-entry.interface';
@Component({
selector: 'app-dossier-correspondence',
templateUrl: './dossier-correspondence.component.html',
})
export class DossierCorrespondenceComponent implements OnInit {
allCorrespondence: Array<DossierEntry>;
correspondenceEntries: Array<DossierEntry>;
attachmentEntries: Array<DossierEntry>;
message = '';
emptyMessage = 'Geen correspondentie.';
errorMessage = 'Er ging iets mis met de connectie. Probeer over enkele minuten nogmaals.';
correspondenceLoaded = false;
showingSingle = false;
single: DossierEntry;
constructor(private healthAPIService: HealthAPIService) {}
handleCorrespondenceLoad(result) {
if (result && result.length === 0) {
this.message = this.emptyMessage;
return;
}
this.allCorrespondence = result;
this.attachmentEntries = [];
this.correspondenceEntries = [];
for (let entry of result) {
switch (entry.type) {
case 'correspondence': {
this.correspondenceEntries.push(entry);
break;
}
case 'attachments': {
this.attachmentEntries.push(entry);
break;
}
default: {
console.log('Dossier correspondence heeft een invalide entry soort teruggegeven');
break;
}
}
}
}
gotoItem(index, type: string) {
this.showingSingle = true;
// this.single = this.allCorrespondence[index];
switch (type) {
case 'correspondence': {
this.single = this.correspondenceEntries[index];
break;
}
case 'attachments': {
this.single = this.attachmentEntries[index];
break;
}
default: {
console.log('Er is op een ongeldige soort dossier entry geklikt');
break;
}
}
}
goBack(event) {
this.showingSingle = false;
}
ngOnInit() {
this.healthAPIService.getDossierEntry('correspondence').subscribe(result => {
console.log(result);
this.handleCorrespondenceLoad(result), (this.correspondenceLoaded = true);
}, msg => (this.message = this.errorMessage));
}
}
然后我会得到这个错误:
dossier-correspondence.component.html:6 ERROR TypeError: Cannot read property 'length' of undefined
at Object.updateDirectives (dossier-correspondence.component.html:8)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:30537)
at checkAndUpdateView (core.js:29933)
at callViewAction (core.js:30174)
at execComponentViewsAction (core.js:30116)
at checkAndUpdateView (core.js:29939)
at callViewAction (core.js:30174)
at execEmbeddedViewsAction (core.js:30137)
at checkAndUpdateView (core.js:29934)
at callViewAction (core.js:30174)
在这一行:
<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>
这是应用程序正在加载组件的代码:
export class IsLoadingComponent implements OnInit {
@Input() message: string;
public text: string;
constructor() {}
ngOnInit() {
this.text = this.message + '...';
}
}
谢谢
现在还有 html 文件:
<app-vital10-page [noTopBar]="true">
<h2 class="section-header">Correspondentie</h2>
<p class="data-entry" *ngIf="!allCorrespondence">{{ message }}</p>
<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>
<div *ngIf="!showingSingle && correspondenceEntries.length > 0">
<div class="main-row main-row-dossier">
<section class="data-entry">
<h3 class="dossier-header">Algemeen</h3>
<table class="dossier-table">
<thead class="dossier-tableheader">
<tr>
<th class="dossier-tablehead fixed-one-fifth">Datum</th>
<th class="dossier-tablehead fixed-four-fifths">Onderwerp</th>
</tr>
</thead>
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of correspondenceEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>
<div *ngIf="!showingSingle && attachmentEntries.length > 0">
<div class="main-row main-row-dossier">
<section class="data-entry">
<h3 class="dossier-header">Bijlage</h3>
<table class="dossier-table">
<thead class="dossier-tableheader">
<tr>
<th class="dossier-tablehead fixed-one-fifth">Datum</th>
<th class="dossier-tablehead fixed-four-fifths">Onderwerp</th>
</tr>
</thead>
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of attachmentEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>
<app-dossier-correspondence-item
[item]="single"
(goBack)="goBack($event)"
*ngIf="showingSingle">
</app-dossier-correspondence-item>
</app-vital10-page>
但是我会在这一行收到错误:
<div *ngIf="!showingSingle && correspondenceEntries && correspondenceEntries.length > 0">
html 文件现在看起来像这样:
<app-vital10-page [noTopBar]="true">
<h2 class="section-header">Correspondentie</h2>
<p class="data-entry" *ngIf="!allCorrespondence">{{ message }}</p>
<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>
<div *ngIf="!showingSingle && correspondenceEntries && correspondenceEntries.length > 0">
<div class="main-row main-row-dossier">
<section class="data-entry">
<h3 class="dossier-header">Algemeen</h3>
<table class="dossier-table">
<thead class="dossier-tableheader">
<tr>
<th class="dossier-tablehead fixed-one-fifth">Datum</th>
<th class="dossier-tablehead fixed-four-fifths">Onderwerp</th>
</tr>
</thead>
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of correspondenceEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>
<div *ngIf="!showingSingle && attachmentEntries.length > 0">
<div class="main-row main-row-dossier">
<section class="data-entry">
<h3 class="dossier-header">Bijlage</h3>
<table class="dossier-table">
<thead class="dossier-tableheader">
<tr>
<th class="dossier-tablehead fixed-one-fifth">Datum</th>
<th class="dossier-tablehead fixed-four-fifths">Onderwerp</th>
</tr>
</thead>
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of attachmentEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>
<app-dossier-correspondence-item
[item]="single"
(goBack)="goBack($event)"
*ngIf="showingSingle">
</app-dossier-correspondence-item>
</app-vital10-page>
出现错误:
dossier-correspondence.component.html:8 ERROR TypeError: Cannot read property 'length' of undefined
at Object.updateDirectives (dossier-correspondence.component.html:30)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:30537)
at checkAndUpdateView (core.js:29933)
at callViewAction (core.js:30174)
at execComponentViewsAction (core.js:30116)
at checkAndUpdateView (core.js:29939)
at callViewAction (core.js:30174)
at execEmbeddedViewsAction (core.js:30137)
at checkAndUpdateView (core.js:29934)
at callViewAction (core.js:30174)
最佳答案
尝试更改模板片段
<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>
<div *ngIf="!showingSingle && correspondenceEntries.length > 0">
到
<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>
<div *ngIf="!showingSingle && correspondenceEntries && correspondenceEntries.length > 0">
您似乎正在尝试访问 correspondenceEntries.length
,但它尚未定义
关于javascript - 错误类型错误 : Cannot read property 'length' of undefined at Object. updateDirectives,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57184666/
我是一名优秀的程序员,十分优秀!