Angular错误错误 TS2532:对象可能为’undefined’。

huangapple go评论135阅读模式
英文:

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.

Angular错误错误 TS2532:对象可能为’undefined’。

英文:

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 &#39;moment&#39;;
import {ITaskUsergen} from &quot;app/shared/model/task.usergen.model&quot;;

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 &quot;app/shared/model/task.model&quot;;
import {MetaTime} from &quot;app/shared/model/meta-time.model&quot;;

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 &#39;moment&#39;;

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
  ) {}
}

Angular错误错误 TS2532:对象可能为’undefined’。

The full error is:

ERROR in src/main/webapp/app/entities/plan-usergen/plan-detail.component.html:47:46 - error TS2532: Object is possibly &#39;undefined&#39;.
47   &lt;td&gt;{{ task.metaTime.taskStart }}&lt;/td&gt;
                                            ~~~~~~~~
src/main/webapp/app/entities/plan-usergen/plan-detail.component.ts:7:16
7   templateUrl: &#39;./plan-detail.component.html&#39;,
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:

&lt;td&gt;{{ task.metaTime?.taskStart }}&lt;/td&gt;

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.

huangapple
  • 本文由 发表于 2020年8月12日 04:56:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63366252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定