英文:
Angular Error error TS2532: Object is possibly 'undefined'
问题
I have been unable to find why I am getting this error on an Angular project.
Plan contains an array of Tasks. Each Task contains an entity called MetaTime. MetaTime has a field TaskStart.
plan.usergen.model.ts
import { Moment } from 'moment';
import { ITaskUsergen } from "app/shared/model/task.usergen.model";
export interface IPlan {
id?: number;
name?: string;
startDate?: Moment;
tasks?: ITaskUsergen[];
}
export class Plan implements IPlan {
constructor(
public id?: number,
public name?: string,
public startDate?: Moment,
public tasks?: ITaskUsergen[],
) {}
}
task.usergen.model.ts
import { ITask, Task } from "app/shared/model/task.model";
import { MetaTime } from "app/shared/model/meta-time.model";
export interface ITaskUsergen extends ITask {
metaTime?: MetaTime;
}
export class TaskUserGen extends Task implements ITaskUsergen {
constructor(public metaTime?: MetaTime) {
super();
}
}
meta-time.model.ts
import { Moment } from 'moment';
export interface IMetaTime {
id?: number;
taskStart?: Moment;
taskDuration?: number;
taskInterval?: number;
taskRepeat?: number;
}
export class MetaTime implements IMetaTime {
constructor(
public id?: number,
public taskStart?: Moment,
public taskDuration?: number,
public taskInterval?: number,
public taskRepeat?: number
) {}
}
The full error is:
ERROR in src/main/webapp/app/entities/plan-usergen/plan-detail.component.html:47:46 - error TS2532: Object is possibly 'undefined'.
47 <td>{{ task.metaTime.taskStart }}</td>
~~~~~~~~
src/main/webapp/app/entities/plan-usergen/plan-detail.component.ts:7:16
7 templateUrl: './plan-detail.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component PlanDetailComponent.
英文:
I have been unable to find why I am getting this error on a Angular project.
Plan contains and array of Tasks. Each Task contains an entity called MetaTime. MetaTime has a field TaskStart.
plan.usergen.model.ts
import { Moment } from 'moment';
import {ITaskUsergen} from "app/shared/model/task.usergen.model";
export interface IPlan {
id?: number;
name?: string;
startDate?: Moment;
tasks?: ITaskUsergen[];
}
export class Plan implements IPlan {
constructor(
public id?: number,
public name?: string,
public startDate?: Moment,
public tasks?: ITaskUsergen[],
) {}
}
task.usergen.model.ts
import {ITask, Task} from "app/shared/model/task.model";
import {MetaTime} from "app/shared/model/meta-time.model";
export interface ITaskUsergen extends ITask {
metaTime?: MetaTime;
}
export class TaskUserGen extends Task implements ITaskUsergen {
constructor(public metaTime?: MetaTime) {
super();
}
}
meta-time.model.ts
import { Moment } from 'moment';
export interface IMetaTime {
id?: number;
taskStart?: Moment;
taskDuration?: number;
taskInterval?: number;
taskRepeat?: number;
}
export class MetaTime implements IMetaTime {
constructor(
public id?: number,
public taskStart?: Moment,
public taskDuration?: number,
public taskInterval?: number,
public taskRepeat?: number
) {}
}
The full error is:
ERROR in src/main/webapp/app/entities/plan-usergen/plan-detail.component.html:47:46 - error TS2532: Object is possibly 'undefined'.
47 <td>{{ task.metaTime.taskStart }}</td>
~~~~~~~~
src/main/webapp/app/entities/plan-usergen/plan-detail.component.ts:7:16
7 templateUrl: './plan-detail.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component PlanDetailComponent.
答案1
得分: 8
这可能是因为你正在进行AOT(提前编译),而metaTime
是可空的(在metaTime?
后面有一个问号)。
编译器报错是因为它相当严格(我自己也不是完全了解Angular的专家),但你应该进行空值检查以防止可能的空值情况。
你可以至少通过使用以下方式修复它,问题是这是否是你期望发生的行为:
<td>{{ task.metaTime?.taskStart }}</td>
最终,关键是要编写能够如你期望的方式运行的代码。
英文:
It's probably because you're compiling AOT (Ahead of Time) and the metaTime
is nullable (the questionmark after metaTime?
).
The compiler gives the error because it's pretty strict I think (not quite an angular expert myself) but you should do a null-check to prevent the possible null situation.
You can at least fix it by using this, the question is whether this is the behavior YOU expect to happen:
<td>{{ task.metaTime?.taskStart }}</td>
In the end it's all about having code that behaves like you would expect.
答案2
得分: 1
由于您将导致 taskStart
的所有变量链都变成了可选项,从技术上讲,它们中的任何一个都可能是未定义的,这将使得访问 taskStart
变量变得不可能。您应该以一种空值安全的方式访问它,或者重新考虑这些变量是否需要是可选的。
英文:
Since you've made the entire chain of variables leading up to taskStart
optional, technically any of them could be undefined, which would make it impossible to access the taskStart
variable. You should either access it in a null-safe way or re-think weather the variables need to be optional.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论